tags:

views:

927

answers:

2

I recently read this thread on MSDN. So I was thinking of using a lamda expression as a way of calling EndInvoke just as a way to make sure everything is nice and tidy. Which would be more correct?

example 1:

Action<int> method = DoSomething;

method.BeginInvoke(5, (a)=>{method.EndInvoke(a);}, null);

Example 2:

Action<int> method = DoSomething;

method.BeginInvoke(5, (a)=>
                                  {
                                      Action<int> m = a.AsyncState as Action<int>;
                                      m.EndInvoke(a);
                                  }, method);
+2  A: 

You might want to read this thread on Haacked's blog. Haven't had a chance to test it, but the gist is in one of the last lines:

ThreadPool.QueueUserWorkItem(callback => im.Send(to, from, subject, body));
borisCallens
I actually have read that article, and it seems kind of redundant to use the QueueUserWorkItem if you can pass in a lambda in the call to BeginInvoke.
Mike_G
QueueUserWorkItem can provide other advanced thread pool management features besides calling EndInvoke. It is preferred to use that.
configurator
+1  A: 

Your 2nd example is slightly more efficient because the "method" delegate instance doesn't have to be captured in the closure. I doubt you'd ever notice.

Hans Passant