2012-06-18 5 views
0

Get이라는 메서드와 C#으로 작성된 X라는 콜백 메서드가 있습니다. Get 메서드에서 _name을 반환해야합니다. 그러나 콜백이 끝난 후 _name의 실제 값을 가져 오는 것만 가능합니다. 콜백이 끝날 때까지 *의 시점에서 멈춰야합니다. 그 후에 만 ​​_name을 반환 할 수 있습니다. 따라서 콜백이 완료되었는지 여부를 확인해야합니다.asyncronus 호출의 콜백이 C#에서 완료되었는지 여부를 찾는 방법은 무엇입니까?

어떻게 위의 시나리오에 대한 해결책을 얻을 수 있습니까? 어떤 일이

내 방법에 대한 솔루션을 제안시겠습니까이

string _name ; 

public string Get() 
{ 
    //Some Statements 
    //Asynchronous call to a method and its call back method is X 


    *Want to stop here until the Call back is finished 

    return _name ; 
} 

private void X (IAsyncResult iAsyncResult) 
{ 
    //Call Endinvoke and get the result 
    //assign the final result to global variable _name 
} 

답변

1

를 참조하십시오.

string _name; 
    ManualResetEvent _completed = new ManualResetEvent(); 

    public string Get() 
    { 
    //Some Statements 
    _completed.Reset(); 
    //Asynchronous call to a method and its call back method is X 


    //*Want to stop here until the Call back is finished 
    _completed.WaitOne(); 

    return _name ; 
    } 

    private void X (IAsyncResult iAsyncResult) 
    { 
    //Call Endinvoke and get the result 
    //assign the final result to global variable _name 
    _completed.Set(); 
    } 
+0

고마워요. 다시 작동합니다. 다시 한 번 감사드립니다. – Thabo

2

같은이 IAsyncResult.AsyncWaitHandle에서 대기 핸들을 사용하여() WaitOne을 호출 할 수 있습니다. 그것이 완료 될 때까지 차단 될 것입니다. 보다 완전한 예를 들어

IAsyncResult result = Something.BeginWhatever(); 
    result.AsyncWaitHandle.WaitOne(); //Block until BeginWhatever completes 

완료를 표시하기 위해으로 ManualResetEvent를 사용하여 MSDN 문서 Blocking Application Execution Using an AsyncWaitHandle

+0

훨씬 더 좋습니다. 고마워. –

+0

예 .. 나는 이미 콜백이 실행될 때까지 기다리고있다.하지만 끝날 때까지 기다리지 말기 바란다. – Thabo

관련 문제