2012-04-06 12 views
0

Form1リストビューに別のスレッドをメールフォームで挿入しようとしていますが、何とか動作しません。ここに私のコードです:別のスレッドからリストビューに挿入する

private delegate void InsertIntoListDelegate(string email); 
    private void InsertIntoList(string email) 
    { 
     if (f1.listView1.InvokeRequired) 
     { 
      f1.listView1.Invoke(new InsertIntoListDelegate(InsertIntoList), email); 
     } 
     else 
     { 
      f1.listView1.Items.Add(email); 
      f1.listView1.Refresh(); 
     } 
    } 

私はあなたにお手伝いできる場合はありがとうございます。

+0

'それが何をして動作しませんwork'ないのですか?どのようなエラーが出ますか? –

+0

エラーがないので、Listviewに何も追加されません。 – Jason

+0

if(!f1.InvokeRequired)throw new Exception( "無効なフォームオブジェクト"); –

答えて

1

これを試してみてください:

private delegate void InsertIntoListDelegate(string email); 
    public void InsertIntoList(string email) 
    { 
     if(InvokeRequired) 
     { 
      Invoke(new InsertIntoListDelegate(InsertIntoList), email); 
     } 
     else 
     { 
      f1.listView1.Items.Add(email); 
      f1.listView1.Refresh(); 
     } 
    } 

InsertIntoListはよう制御しませリストビューで呼び出されるべきで囲みコントロールのメンバーです。

は私のために働く、この非常に簡単なテストをお試しください:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private delegate void InsertIntoListDelegate(string email); 

    public void InsertIntoList(string email) 
    { 
     if(InvokeRequired) 
     { 
      Invoke(new InsertIntoListDelegate(InsertIntoList), email); 
     } 
     else 
     { 
      listView1.Items.Add(email); 
     } 
    } 

    private void button1_Click(object sender, System.EventArgs e) 
    { 
     Task.Factory.StartNew(() => InsertIntoList("test")); 
    } 
} 
+0

Hey Phil、なんとか動作しません。それとも、うまくいけばListviewをリフレッシュしません:( – Jason

+0

なぜ、うまくいかなかったのか分かりませんが、listviewをrichtextboxに変更しても問題なく動作します。 – Jason

関連する問題