2010-08-23 2 views
0

이것은 C# 질문입니다. 비동기 이벤트 콜백에 대한 동기식 호출을 수행하려고합니다. C# : 이벤트를 비동기 적으로 캡처하고 함수에서 값을 동 기적으로 반환하는 방법

class User 
{ 
    EventHandler Service1Started() 
    { 
     "Looks like service 1 started as I got event from server class" ; 
    } 
    EventHandler Service2Started() 
    { 
     "Looks like service 2 started as I got event from server class" ; 
    } 
    public void StartServicesOnlyStartService2AfterService1Started() 
    { 
     Server server = new Server(); 

     // want to call these 2 methods synchronously here 
     // how to wait till service1 has started thru the event we get 
     // Want to avoid polling, sleeping etc if possible 

     server.StartService1();    
     server.StartService2(); 

    } 
} 

답변

3

귀하의 코드는 매우 불분명하다 REQ에 대한 코드의 주석을 볼 수 있지만 가장 간단한 방법은 서비스 1 서비스 2를 시작하기위한 이벤트 핸들러을하는 것입니다 :

server.Service1Started += delegate { server.StartService2(); }; 
+0

변경된 질문이 변경되었습니다. 폴링 등을 사용하지 않고 동 기적으로 두 가지 방법 을 호출해야합니다. –

+0

@john appleseed : 질문이 이전에 상당히 불분명했습니다. 이 경우 정확하게 * 동기 *를 의미합니까? 질문을 훨씬 명확하게해야합니다. 당신의 메소드'Service1Started'와'Service2Started'는 이벤트 핸들러에 이상한 서명을 가지고 있습니다. 단지 문자열 리터럴을 가지고 있습니다. 기본적으로 모든 것이 엉망입니다. 질문을 더 분명하게 표현할수록 우리가 실제로 당신을 도울 수있는 기회가 더 많아집니다. –

+0

죄송합니다. 나는 내가 필요한 것을 더 잘 설명 할 필요가 있다고 생각한다. 또한 Rx (Reactive extensions)를 둘러 보는 것은 솔루션 후보로 보이지만 매우 복잡해 보입니다. 고맙습니다. –

2

존의 대답이 옳다 delegate :

EventHandler Service1Started() 
{ 
    // Looks like service 1 started as I got event from server class 
    server.StartService2(); 
} 
관련 문제