Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been doing some reading tonight on async delegate method invocations and I'm not too clear on what's going on under the hood with delegate.BeginInvoke() but I wanted to know if someone can tell me if there are any performance differences between these two code blocks as far as using an AsyncCallback and not using an AsyncCallback and take into consideration that I'm updating the GUI winForm intensively (multiple times per sec):

public delegate string ProcDataDel(string data);

public ProcDataDel procData = new ProcDataDel(ProcDataMethod);
public AsyncCallBack procDataDone = new AsyncCallBack(ProcDataDoneMethod);

public string ProcDataMethod(string data)
{
   return data + " has been processed...";
}

public void ProcDataDoneMethod(IAsyncResult result)
{
   ProcDataDel _procData = (ProcDataDel)((AsyncResult)result).AsyncDeleate;
   string data = _proData.EndInvoke(result);
   
   if (listView1.InvokeRequired)
   {
      listView1.Invoke(new MethodInvoker(delegate() {listView1.Add(data);}));
   }
   else
      listView1.Add(data);
}

public void Button1_ClickEvent(object sender, EventArgs e)
{
   procData.BeginInvoke("some data",procDataDoneMethod,procDataMethod);
}


Without AsyncCallback:
public delegate void ProcDataDel(string data);

public void ProcDataFunc(string data)
{
   if (listView1.InvokeRequired)
   {
       listView1.Invoke(new MethodInvoker(delegate() {listView1.Add(data);}));
   }
   else
      listView1.Add(data);
}

public void Button1_ClickEvent(object sender, EventArgs e)
{
   listView1.BeginInvoke(new ProcDataDel(ProcDataFunc),new object[]{"some data has been processed..."});
}


I'm using the bottom code in a production project I'm working on but I'm having performance issues with my GUI being updated so often seems not to be keeping up and a little unresponsive...I was wondering if the code using the AsyncCallback was any different as far a performance goes.

Any feedback would be appreciated! Thanks in advance and HAPPY NEW YEAR!
Posted
Updated 31-Dec-12 23:05pm
v5
Comments
Michiel du Toit 1-Jan-13 14:46pm    
Hi Donald, I think you're actually looking for Delegate.BeginInvoke (which uses a threadpool thread). Control.BeginInvoke posts to the control's message and thus does not utilize a seperate thread.

A great article on the topic:

http://www.codeproject.com/Articles/10311/What-s-up-with-BeginInvoke

A thread that does the processing and then invokes the UI to update progress might be a more intuitive solution to your problem?

Regards
d.allen101 2-Jan-13 2:00am    
thanks michiel! i didn't know that control.beginvoke doesn't use thread.pool i thought thats where the asychronous functionality was coming from. thanks again!

1 solution

I tried to explain it in my past answers:

Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

There difference between BeginInvoke and Invoke is this: first returns immediately, before delegate instance goes in the queue of the UI thread, got invoked and returns the result. So, the result is delivered in a deferred manner.

Also, you get a good chance to look under the hood if you read my small article where I implement invocation mechanism for a custom (other than UI) threads. It is complete with source code, explanation and usage samples:
Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

Happy New Year!

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900