2012-12-05 2 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

왜 필요한가요? 완료 될 때까지 기다려야합니까? 즉,이 같은 작품 : 당신이 기다리고 사용하고 정말 이벤트와 같은 패턴을 사용할 필요가없는 경우, ContinueWith를 사용

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 
} 

. 이를 작업 완료를위한 이벤트 처리기를 추가하는 것으로 생각할 수 있습니다.

+0

감사합니다 ... "task.ContinueWith (...);" 내가 원했던 것이 .. – Sency