开发者

what's the best way to call a normal method in async way?

Just have a normal method like

void DosomethingLong()

What's the best way to ca开发者_如何学编程ll it in an async way?


The standard way is to create a delegate of the method:

Action myMethod = DosomethingLong;

then execute it asynchronously using the APM (example):

IAsyncResult result = myMethod.BeginInvoke(..., null);

// ...

myMethod.EndInvoke(result);

There are other methods you can use; using a Thread, using BackgroundWorker, etc, depending on your exact requirements.


If using .NET 4 you could do

var yourLongRunningTask = Task.Factory.StartNew(DosomethingLong);
// some time later
Task.WaitAll(yourLongRunningTask);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜