Incompatibility of the C # parameter count while trying to add AsyncCallback in BeginInvoke ()

advertisements

I have main form (PrenosForm) and I am trying to run Form2 asynchronously.

  1. It works without callback delegate:

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

     this.BeginInvoke(cp, new object[] { datoteke, this.treeView1.SelectedNode.FullPath.ToString(), this, efekt }, new AsyncCallback(callBackDelegate), null); //doesn't work parameter count mismatch 2.
    
    
  3. Works with 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 way work and the other doesn't? I'm new at 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.

}


It is because Control.BeginInvoke() has a completely different signature from SomeDelegate.BeginInvoke(). While their method names are the same, they are fundamentally different methods. And fundamentally work differently at runtime, there is no comparison.

Control.BeginInvoke() takes a delegate and an object[]. Cast in stone.

private delegate SomeDelegate(mumble, foo, bar) automatically creates a SomeDelegate.BeginInvoke() method. Whose signature takes those three arguments, plus two extra arguments, a callback and a state object.

A significant runtime difference is that Control.BeginInvoke() can call a delegate and if it bombs with an exception then the exception is raised on the UI thread. A delegate's BeginInvoke() method doesn't do this, it re-raises the exception in the callback that calls EndInvoke().

Very confuzzling, I know, maybe they shouldn't have used the same name.