tags:

views:

50

answers:

1

In a multi-threaded application, what is the performance impact of writing something like this:

TestClass t = new TestClass();
ThreadPool.QueueUserWorkItem(x=>DoSomething(t));

Is there any difference if i write it like this:

TestClass t = new TestClass();
ThreadPool.QueueUserWorkItem(x=>{
                                   TestClass t2 = x as TestClass;
                                   DoSomething(t2);

                                }, t);

And while im at it how about this:

  TestClass t = new TestClass();
  Action<TestClass> someAction = DoSomething;
  someAction.BeginInvoke(t, asyncResult=>{
                                             Action<TestClass> a = asyncResult.State as Action<TestClass>;
                                             a.EndInvoke(asyncResult);
                                         }, someAction);

In a somewhat related question, do all these pretty much do the same thing under the hood?

+1  A: 

TEST IT! A couple of quick benchmarks should give you the answer.

I would speculate the difference is probably so small as to not matter unless you are doing this operation thousands of times, but you might as well test it to be sure.

Stephan