简体   繁体   English

EventWaitHandle waitOne() 导致我的工作线程挂起直到超时

[英]EventWaitHandle waitOne() causes my worker thread to suspend until it has timed out

I am using a EventWaitHandle() handler.我正在使用 EventWaitHandle() 处理程序。 This handler is called in a ButtonClick event that waits for 10 seconds.此处理程序在等待 10 秒的 ButtonClick 事件中调用。 There is another worker thread which upon receiving some data call Set() on the handler.还有另一个工作线程在处理程序上接收到一些数据调用 Set() 时。 Problem is the WaitOne() returns false after the timeout occurs.问题是 WaitOne() 在超时发生后返回 false。 The worker thread doesnt run and looks like its suspended, hence Set() is not called.工作线程没有运行并且看起来像是挂起的,因此没有调用 Set()。 Once the timeout is over, my worker thread resumes and Set() method is called.超时结束后,我的工作线程将恢复并调用 Set() 方法。 To verify I tried without the EventWaitHandle() to check if my worker thread actually takes 10 seconds of time, but it didnt, and Set() method had hit immediately.为了验证,我尝试在没有 EventWaitHandle() 的情况下检查我的工作线程是否实际需要 10 秒的时间,但它没有,并且 Set() 方法立即命中。 I am not sure why the worker thread runs after the timeout has occurred in the I am new to C#.我不知道为什么工作线程在我是 C# 新手中发生超时后运行。 Thanks in advance提前致谢

MainWindow.xaml.cs主窗口.xaml.cs

public static EventWaitHandle autoResetEvent = new EventWaitHandle(false, EventResetMode.AutoReset);

XYZDialogBox.cs XYZDialogBox.cs

private void BtnConnect_Click(object sender, RoutedEventArgs e)
{
MainWindow.autoResetEvent.Reset();
if (!MainWindow.autoResetEvent.WaitOne(10000))
                {
                    //Line number details is not received from Service
                    MessageBox.Show("Timeout");

                    //now disconnect and exit
                    strCommand = "#cmddisconnect " + tbIPAddress.Text + " #";
                    tcpClient.AddCommandAsync(strCommand);
                    return;
                }
}

ABC.cs ABC.cs

public void ABC(ref string strData)
{
   while(strData != "")
   {
     //do something
     MainWindow.autoResetEvent.Set();
   }
}

Using the async await pattern based on your code:根据您的代码使用异步等待模式:

private async void BtnConnect_Click(object sender, RoutedEventArgs e)
{
    // await frees up the main thread but halts execution of this method until finished.
    var result  = await Task.Run(() => 
     {
          //Do something that takes time.
          // This is now in another thread.
          return MyService.ServiceCall(arg);
     });

     // use result here synchronously.
}

As an aside the example here is very rudimentary case of using the async await pattern.顺便说一句,这里的示例是使用异步等待模式的非常基本的情况。 Ideally your service would have a library that implements an async method call that returns an awaitable task.理想情况下,您的服务应该有一个实现异步方法调用的库,该调用返回一个可等待的任务。 That would look something like this.那看起来像这样。

private async void BtnConnect_Click(object sender, RoutedEventArgs e)
{
    bool success = await MyService.ServiceCallAsync(args); // async service call.

    if(success)
    {
       // handle
       return;
    }

    // an error occurred.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM