WCF异步 - 如何使用ManualResetEvent

时间:2011-12-01 00:37:36

标签: wcf manualresetevent

任何人都可以告诉我如何在异步wcf服务中使用'ManualResetEvent'吗?我有一个控制台应用程序调用异步wcf服务,我想在'oncomplete'事件结束后关闭控制台应用程序。

如果可能,请提供样品。

提前致谢。

1 个答案:

答案 0 :(得分:2)

您可以编写如下控制台应用程序:

class Program
{
    static ManualResetEvent exitEvent = new ManualResetEvent(false); // Create the wait handle

    static void Main()
    {
        using(var client = CreateYourClient())
        {
            client.MethodCompleted += MethodCompleted;
            client.MethodAsync(); // Start method

            exitEvent.WaitOne(); // Block until the method is done...
        } 
    }

    static void MethodCompleted(object sender, MethodCompletedEventArgs args)
    {
       // Do your work...

       // At this point, signal that the console can close...
       exitEvent.Set();
    }
}

但是,如果您只是进行单个方法调用,那么最好让它同步。如果你同时调用多个异步方法,这只会非常有用。

相关问题