サーバーからデータをダウンロードする Windows ベースのアプリケーションを作成しています。これらのダウンロード操作を実行するために別のクラスで作成されたバックグラウンド スレッドを使用しています。そして、リッチ テキスト ボックス、つまりメイン スレッドでダウンロード ステータスを継続的に表示したいのですが、これを行うことができません。クロススレッド操作が無効です。 . この問題を解決するのを手伝ってください。
Form1.cs のメソッド
public void UpdateRichText(string Text)
{
SetRichText(Text);
}
public delegate void SetRichTextTextDelegate(string text);
public void SetRichText(object number)
{
if (InvokeRequired)
{
this.BeginInvoke(new SetRichTextTextDelegate(SetRichText),text);
return;
}
richTextBox1.Text += number.ToString() + "\n";
}
private void button3_Click_1(object sender, EventArgs e)
{
demo d = new demo();
d.display();
}
demo.cs のメソッド
Form1 f = new Form1();
public void display()
{
Thread t = new Thread(new ThreadStart(call));
t.Start();
}
public void call()
{
//when i call this method every time if(InvokeRequired) is false.
f.UpdateRichText("Called from Thread");
}