The shortest way to call the void method asynchronously

I have a method that I would like to call asynchronously:

void Foo() { } 

I can actually call this asynchronously as follows:

 delegate void DVoidMethod(); DVoidMethod FooDelegate = new DVoidMethod(Foo); FooDelegate.BeginInvoke(null,null); 

Does anyone have alternatives?

I think three lines of code are too many?

+4
source share
6 answers

Denial of responsibility:

Do not use this in real code. This is just an attempt to shorten the mentioned OP code. To make true asynchronous calls without returning results, use:

 ThreadPool.QueueUserWorkItem(stateObject => Foo()); 

Use Action and Func delegates as part of:

 new Action(Foo).BeginInvoke(null, null); 

Action<T> existed since release 2.0. Other options are added in 3.5. In 2.0, you can, however, declare a bunch of them somewhere manually for future reference; or even better, use LINQBridge .

+10
source

What about:

 ThreadPool.QueueUserWorkItem(new WaitCallback((o) => Foo())); 

Update the TPL API account available in newer versions of .net:

In .Net 4 and higher, you can:

 Task.Factory.StartNew(() => Foo()); 

In .Net 4.5 and higher, you can also:

 Task.Run(() => Foo()); 

The difference between Task.Factory.StartNew and Task.Run is that the "StartNew" method has some additional parameters that allow you to pass state, task creation parameters and a scheduler to give you additional control if you need it.

+8
source

You can knock it down to two lines (in .Net 1.1 and earlier) with a simple one:

 delegate void DVoidMethod(); new FooDelegate(DVoidMethod).BeginInvoke(null, null) 

But in version 2.0 and later, Mehrdad syntax works.

Contact your Action delegate for version 2.0 and later on MSDN at:

http://msdn.microsoft.com/en-us/library/018hxwa8(VS.80).aspx

+3
source

Without using Action and Func, you can use MethodInvoker in case of void () delegation:

 new MethodInvoker(Foo).BeginInvoke(null, null); 

In addition, it should be noted that if you use the BeginInvoke method, you must call EndInvoke when the delegate has finished executing .. so this line above should either change to using the callback, or you need to save the delegate link .. Like this:

 MethodInvoker mi = null; IAsyncResult ar = null; ar = (mi = new MethodInvoker(Foo)).BeginInvoke(null,null); .. sometime later after the delegate has completed executing mi.EndInvoke(ar); 

This is true if you are using BeginInvoke. Therefore, I think in the end I would not recommend using the BeginInvoke method.

@Steven's solution using ThreadPool is a much better solution in my opinion .. and its clearly :)

+3
source

Strictly speaking, I would not worry about how many lines of code something takes in terms of performance, because you can not imagine what actual code is behind the functions that you call.

+1
source

An old thread, but I could not stay on it.

You really should call EndInvoke to avoid leaks, even if you are not using the result or are not responding to the completion of the method. But this does not mean that it should be ugly.

BeginInvoke expects AsyncCallback as a callback parameter. An AsyncCallback signature is a void that accepts IAsyncResult. EndInvoke has such a signature, therefore:

  //waste some time... void Foo() {} void InvokeFoo() { Action fooer = () => Foo(); fooer.BeginInvoke(fooer.EndInvoke, null); } void InvokeFoo2() { // or, less concise but some consider more readable: Action fooer = new Action(Foo); //...invoke doesn't change } 
+1
source

All Articles