开发者

C# : How to capture events asynchronously and return a value from a function synchronously

This is a C# question I want to do a synchronous call for asynchronous event call backs. See comment in code for the req

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();

    }
}


Your code is pretty unclear, but the simplest approach would just be to make the event handler for Service1 start service 2:

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


Jon's answer is right, this is the equivalent in case it's not convenient to use delegate:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜