2012-12-05 71 views
3

我有一个这样的异步方法C#5异步方法完成事件。

public async void Method() 
{ 
    await // Long run method 
} 

当林调用这个方法我可以当这个方法完成了事件?

public void CallMethod() 
{ 
    Method(); 
    // Here I need an event once the Method() finished its process and returned. 
} 

回答

7

为什么你需要那个?你需要等待完成吗?这是这样的:

public async Task Method() //returns Task 
{ 
    await // Long run method 
} 

public void CallMethod() 
{ 
    var task = Method(); 

    //here you can set up an "event handler" for the task completion 
    task.ContinueWith(...); 

    await task; //or await directly 
} 

如果您不能使用等待,真正需要使用一个事件性的模式,使用ContinueWith。您可以将其视为为任务完成添加事件处理程序。

+0

thankx ...“task.ContinueWith(...);”是我想要的东西.. – Sency