Inability to count C # parameters when trying to add AsyncCallback to BeginInvoke ()

I have a main form (PrenosForm) and I'm trying to run Form2 asynchronously.

  • It works without a callback delegate:

    this.BeginInvoke(cp, new object[] { datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt }, null); //works  1.
    
  • Doesn't work with a callback delegate (mismatch of the parameter counter):

     this.BeginInvoke(cp, new object[] { datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt }, new AsyncCallback(callBackDelegate), null); //doesn't work parameter count mismatch 2.
    
  • Works with a callback delegate if I do it like this:

    cp.BeginInvoke(datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt, new AsyncCallback(callBackDelegate), null); //works  3.
    

My question is: why does one work and the other not? I am new to this. Would anyone be so kind as to answer my question and point out my mistakes?

 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.

}
+3
source share
2 answers

, Control.BeginInvoke() SomeDelegate.BeginInvoke(). , . - , .

Control.BeginInvoke() []. .

SomeDelegate (mumble, foo, bar) SomeDelegate.BeginInvoke(). , , .

, Control.BeginInvoke() , , . BeginInvoke() , , EndInvoke().

confuzzling, , , .

+7

. - .

, Control.BeginInvoke .
; AsyncCallback, null.
, .

, Control.BeginInvoke ; , .

0

Source: https://habr.com/ru/post/1783314/


All Articles