Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试将AsyncCallback添加到BeginInvoke()时,C#参数计数不匹配_C#_Multithreading_Invoke - Fatal编程技术网

尝试将AsyncCallback添加到BeginInvoke()时,C#参数计数不匹配

尝试将AsyncCallback添加到BeginInvoke()时,C#参数计数不匹配,c#,multithreading,invoke,C#,Multithreading,Invoke,我有一个主窗体(PrenosForm),我正在尝试异步运行Form2 它在没有回调委托的情况下工作: this.BeginInvoke(cp, new object[] { datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt }, null); //works 1. 无法使用回调委托(参数计数不匹配): 如果我这样做,则使用回调委托: cp.BeginInvoke(datoteke, this.tr

我有一个主窗体(PrenosForm),我正在尝试异步运行Form2

  • 它在没有回调委托的情况下工作:

    this.BeginInvoke(cp, new object[] { datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt }, null); //works  1.
    
  • 无法使用回调委托(参数计数不匹配):

  • 如果我这样做,则使用回调委托:

    cp.BeginInvoke(datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt, new AsyncCallback(callBackDelegate), null); //works  3.
    
  • 我的问题是为什么一种方法有效而另一种方法无效?这方面我是新手。有人能回答我的问题并指出我的错误吗

     private delegate void copyDelegat(List<ListViewItem> datoteke, string path, PrenosForm forma, DragDropEffects efekt);
     private delegate void callBackDelegat(IAsyncResult a);
    
     public void doCopy(List<ListViewItem> datoteke, string path, PrenosForm forma, DragDropEffects efekt)
     {
         new Form2(datoteke, path, forma, efekt);
     }
    
     public void callBackFunc(IAsyncResult a)
     {
         AsyncResult res = a.AsyncState as AsyncResult;
         copyDelegat delegat = res.AsyncDelegate as copyDelegat;
         delegat.EndInvoke(a);
     }
    
    public void kopiraj(List<ListViewItem> datoteke, DragDropEffects efekt)
    {
    
    
     copyDelegat cp = new copyDelegat(doCopy);
     callBackDelegat callBackDelegate = new callBackDelegat(callBackFunc);
     this.BeginInvoke(cp, new object[] { datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt }, new AsyncCallback(callBackDelegate), null); //doesn't work parameter count missmatch 2.
     this.BeginInvoke(cp, new object[] { datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt }, null); //works  1.
     cp.BeginInvoke(datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt, new AsyncCallback(callBackDelegate), null); //works  3.
    
    }
    
    private delegate void copyDelegat(列出日期、字符串路径、PrenosForm forma、DragDropEffects efekt);
    私有委托void callBackDelegat(IAsyncResult a);
    公共作废文件副本(列表日期、字符串路径、PrenosForma格式、DragDropEffects efekt)
    {
    新表格2(datoteke、path、forma、efekt);
    }
    公共无效调用函数(IAsyncResult a)
    {
    AsyncResult res=a.AsyncState作为AsyncResult;
    copyDelegat delegat=res.AsyncDelegate作为copyDelegat;
    委托书(a);
    }
    公共无效kopiraj(列表datoteke,DragDropEffects efekt)
    {
    copyDelegat cp=新的copyDelegat(doCopy);
    callBackDelegat callBackDelegate=新的callBackDelegat(callBackFunc);
    this.BeginInvoke(cp,新对象[]{datoteke,this.treeView1.SelectedNode.FullPath.ToString(),this,efekt},new AsyncCallback(callBackDelegate),null);//不工作参数计数不匹配2。
    this.BeginInvoke(cp,新对象[]{datoteke,this.treeView1.SelectedNode.FullPath.ToString(),this,efekt},null);//有效1。
    cp.BeginInvoke(datoteke,this.treeView1.SelectedNode.FullPath.ToString(),this,efekt,new AsyncCallback(callBackDelegate),null);//工作3。
    }
    
    根本不要这样做。
    在不同的线程上显示多个表单是一个非常糟糕的主意,最终会造成很多麻烦

    您的第二个示例不起作用,因为它不支持回调参数。
    您的代码被解释为使用三个参数调用委托;数组,以及
    AsyncCallback
    null

    由于您的方法不接受此类参数,因此会引发异常


    另外,调用
    Control.BeginInvoke
    不会在后台运行函数;它下次到达消息循环时将在UI线程上运行它。

    这是因为Control.BeginInvoke()与SomeDelegate.BeginInvoke()具有完全不同的签名。虽然它们的方法名称相同,但它们是根本不同的方法。而且在运行时的工作方式根本不同,这是无法比较的

    BeginInvoke()接受委托和对象[]。用石头铸造的

    私有委托SomeDelegate(mumble、foo、bar)自动创建SomeDelegate.BeginInvoke()方法。其签名接受这三个参数,再加上两个额外参数,一个回调和一个状态对象

    运行时的一个显著区别是Control.BeginInvoke()可以调用委托,如果委托发生异常,则会在UI线程上引发异常。委托的BeginInvoke()方法不会执行此操作,它会在调用EndInvoke()的回调中重新引发异常

    非常令人困惑,我知道,也许他们不应该用同一个名字

     private delegate void copyDelegat(List<ListViewItem> datoteke, string path, PrenosForm forma, DragDropEffects efekt);
     private delegate void callBackDelegat(IAsyncResult a);
    
     public void doCopy(List<ListViewItem> datoteke, string path, PrenosForm forma, DragDropEffects efekt)
     {
         new Form2(datoteke, path, forma, efekt);
     }
    
     public void callBackFunc(IAsyncResult a)
     {
         AsyncResult res = a.AsyncState as AsyncResult;
         copyDelegat delegat = res.AsyncDelegate as copyDelegat;
         delegat.EndInvoke(a);
     }
    
    public void kopiraj(List<ListViewItem> datoteke, DragDropEffects efekt)
    {
    
    
     copyDelegat cp = new copyDelegat(doCopy);
     callBackDelegat callBackDelegate = new callBackDelegat(callBackFunc);
     this.BeginInvoke(cp, new object[] { datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt }, new AsyncCallback(callBackDelegate), null); //doesn't work parameter count missmatch 2.
     this.BeginInvoke(cp, new object[] { datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt }, null); //works  1.
     cp.BeginInvoke(datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt, new AsyncCallback(callBackDelegate), null); //works  3.
    
    }