WCF 异步 - 如何使用 ManualResetEvent

发布于 2024-12-19 01:54:24 字数 140 浏览 2 评论 0原文

谁能告诉我如何在异步 wcf 服务中使用“ManualResetEvent”?我有一个控制台应用程序,它调用异步 wcf 服务,我想在“oncomplete”事件完成后关闭控制台应用程序。

如果可能的话请给我提供一个样品。

提前致谢。

Can any one tell me how to use 'ManualResetEvent' in a async wcf service? I have a console application which makes calls to async wcf service and I wanted to close the console app after 'oncomplete' event finishes.

If possible please provide me a sample.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深缘浅 2024-12-26 01:54:24

您将编写类似以下内容的控制台应用程序:

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

但是,如果您只是执行单个方法调用,那么最好使其同步。仅当您同时调用多个异步方法时,这才会真正有用。

You'd write your Console App something like the following:

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

However, if you're just doing a single method call, it's probably better to just make it synchronous. This would only really be beneficial if you're calling multiple asynchronous methods simultaneously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文