2012-01-09 81 views
0

我有2個線程:UI和服務器。如果服務器線程收到一些消息,則必須將其交給UI。這就是我使用invoke-method的原因。當發生c#Invoke()時,我得到一個參數異常

public class Server 
    { 
    private Form1 myForm; 
    private String server; 

    public Client(Form1 myForm1, String serv) 
    { 
    myForm = myForm1; 
    server = serv; 
    } 
    delegate void SetTextCallback(string text, Int16 position); 
    public void connection() 
    {.... 
    try 
     { 
     //Connection succeed 
     //got message 
     SetText(data[0], Convert.ToInt16(data[1])); 
     ... 
     }catch (ArgumentException ae) 
      { 
       ... 
      } 
      catch (SocketException se) 
      { 
       .... 
      } 
    } 
    private void SetText(string text, int position) 
    { 
    if (myForm.InvokeRequired) 
       { 
        SetTextCallback d = new SetTextCallback(
             this.myForm.SetTextToListBox1); 
        myForm.Invoke(d, new object[] { text, position }); 
       } 
    } 

在UI方面我有這個

public void SetTextToListBox1(String text, Int16 position){} 

當我調試,我可以看到,在setText()我得到一個ArgumentException。

+1

有沒有使用BackgroundWorker的理由嗎?請同時展開ArgumentException,是否有消息以及堆棧跟蹤中的內容? – 2012-01-09 14:02:56

回答

0

如果粘貼的代碼是正確和完整的,你必須改變this.myForm.SetTextToListBox1this.myForm.SetText

+0

但我的用戶界面沒有方法SetText()... – Julia 2012-01-09 14:16:56

+0

我有一個smartdevice項目,這就是爲什麼我不能使用backgroundworker。 – Julia 2012-01-09 14:18:42

+0

catch(ArgumentException ae) { actInfo + =「ArgumentNullException:{0}」+ ae; } // actInfo是一個包含所有服務器日誌的字符串 – Julia 2012-01-09 14:21:20

相關問題