Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 31-Dec-12 23:13pm
Edited 1-Jan-13 0:05am
v5
Comments
Michiel du Toit at 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
Donald Allen at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 305
1 OriginalGriff 225
2 Kornfeld Eliyahu Peter 130
3 DamithSL 110
4 Peter Leow 95
0 OriginalGriff 7,355
1 DamithSL 5,199
2 Sergey Alexandrovich Kryukov 4,942
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100