尝试跨线程更新时参数计数不匹配

时间:2012-01-27 02:53:28

标签: c# visual-studio-2010

我试图在MSDN上关注此示例:http://msdn.microsoft.com/en-us/library/a1hetckb.aspx

我想我正在做他们所做的一切,但我一直收到这个错误:参数计数不匹配

这是我的代码:

Form1中:

 namespace ETL
 {
   public partial class Form1 : Form
   {
       private Thread myThread;
       public delegate void delegatePrintoutProcess(string myString);
       public delegatePrintoutProcess myDelegate2;
  ...

    private void startParseToolStripMenuItem_Click(object sender, EventArgs e)
    {
        myDelegate2 = new delegatePrintoutProcess(updatePrintoutMethod);
        myThread = new Thread(new ThreadStart(ThreadFunction));
        myThread.Start();  
    }
    public void updatePrintoutMethod(string text)
    {

       // this.richTextBox1.Text = text;
    }
    private void ThreadFunction()
    {
        parseFile myThreadClassObject = new parseFile(this);
        myThreadClassObject.getFilePath = filePath;
        myThreadClassObject.Run();


    }
}

parseFile类:

 namespace ETL
 {
    public class parseFile
    {
        Form1 myFormControl1;
    public parseFile(Form1 myForm)
    {
        //get a handle on the main form
        myFormControl1 = myForm;
    }
    String myString;
    public void Run()
    {

        for (int i = 1; i <= 5; i++)
        {
            myString = "Step number " + i.ToString() + " executed";
            Thread.Sleep(400);
            // Execute the specified delegate on the thread that owns
            // 'myFormControl1' control's underlying window handle with
            // the specified list of arguments.
            myFormControl1.Invoke(myFormControl1.myDelegate,
                                   new Object[] { myString }); //error here
        }
    }
  }

我很确定我已经按照提供的示例进行操作,所以不确定发生了什么。

由于 杰森

1 个答案:

答案 0 :(得分:2)

猜猜(因为没有要证明的代码) - myDelegate的类型是“带有0或2个参数的函数”,与您可能想要调用的myDelegate2不同。