C#:如何异步捕获事件并同步从函数返回值

C#:如何异步捕获事件并同步从函数返回值,c#,.net,events,asynchronous,polling,C#,.net,Events,Asynchronous,Polling,这是一个C#问题 我想对异步事件回调执行同步调用。 有关req,请参阅代码中的注释 class User { EventHandler Service1Started() { "Looks like service 1 started as I got event from server class" ; } EventHandler Service2Started() { "Looks like service 2 started

这是一个C#问题 我想对异步事件回调执行同步调用。 有关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();

    }
}

您的代码非常不清楚,但最简单的方法是让Service1的事件处理程序启动Service2:

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

您的代码非常不清楚,但最简单的方法是让Service1的事件处理程序启动Service2:

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

Jon的回答是正确的,如果使用
委托不方便,这是等效的:

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

Jon的回答是正确的,如果使用
委托不方便,这是等效的:

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

我改变了问题。需要在不使用轮询的情况下同步调用两个方法etc@john苹果色:你的问题以前很不清楚,现在更不清楚了。在这种情况下,你所说的“同步”到底是什么意思?你需要把你的问题说得更清楚。您的方法
Service1Started
Service2Started
具有事件处理程序的奇数签名,并且只有字符串文本。。。基本上都是一团糟。你的问题表达得越清楚,我们就越有可能真正帮助你。对不起。我想我需要更好地解释我需要什么。另外,环顾Rx(无功扩展)看起来是解决方案的一个很好的候选方案,但看起来非常复杂。约翰:如果你能给我一个更清楚的描述,我很乐意看一看。Rx当然既复杂又可怕:)我改变了问题。需要在不使用轮询的情况下同步调用两个方法etc@john苹果色:你的问题以前很不清楚,现在更不清楚了。在这种情况下,你所说的“同步”到底是什么意思?你需要把你的问题说得更清楚。您的方法
Service1Started
Service2Started
具有事件处理程序的奇数签名,并且只有字符串文本。。。基本上都是一团糟。你的问题表达得越清楚,我们就越有可能真正帮助你。对不起。我想我需要更好地解释我需要什么。另外,环顾Rx(无功扩展)看起来是解决方案的一个很好的候选方案,但看起来非常复杂。约翰:如果你能给我一个更清楚的描述,我很乐意看一看。Rx当然既复杂又可怕:)