我最近发现了使用Threads
在我的应用程序中AutoResetEvent
之间的消息传递。在控制台应用程序中,我会进行while(true)
循环并依赖AutoResetEvent.WaitOne()
等待线程发出信号,例如数据已准备好使用AutoResetEvent.Set()
进行处理。我知道如何使用Class
在线程之间共享数据,该AutoResetEvent
具有工作线程的方法,以及在工作线程和主线程之间共享的数据元素。
我的问题是如何在Winforms应用程序中使用WaitOne()
,我通常看不到命令循环。我应该在Winforms应用程序中将Console.ReadLine()
调用放在哪里?
关于示例代码:最好的示例是关于如何使signals
超时的answer on this site,这基本上是关于如何使用BackgroundWorker
的简单示例。该示例是一个控制台应用程序示例。
在使用Google搜索时,我的问题的答案可能是使用{{1}}控件?
答案 0 :(得分:1)
根据您的问题,我了解您的UI线程应该等待事件。
WinForms应用程序的UI线程必须将控制权传递给操作系统以允许用户交互,因此等待AutoResetEvent
是不合适的。如果要与UI线程通信,可以使用:
bool ret = (bool)myFormInstance.Invoke(
new Func<DataClass, bool>(myFormInstance.DoStuff),
myData);
public class MyForm : Form
{
public bool DoStuff(DataClass data)
{
....
}
}
您还可以使用BeginInvoke
异步运行UI线程上的代码。
如果你想要相反的东西(UI线程告诉工作线程开始处理)你可以使用BackgroundWorker
,应该有很多关于它的教程。
答案 1 :(得分:1)
是的,请改用BackgroundWorker
。
答案 2 :(得分:1)
似乎在Winforms中使用多线程的完整解决方案是将BackgroundWorker
和AutoResetEvent
结合起来。
如果我是正确的,那么BackgroundWorker
就像Thread
,其优点是它与ProgressChanged
事件处理程序很好地接口,用于从后台线程到UI的信令。对于从UI到后台线程的信令,我们可能仍需要AutoResetEvent
。在我的例子中,我想使用Next按钮开始一个新的阻塞调用来接收数据。下面是我刚刚测试的代码。
AutoResetEvent
更好的方法了。似乎工作得很好。
/// <summary>
/// The Next button uses this to signal the BackgroundWorker
/// to start the blocking call to Receive data
/// </summary>
private AutoResetEvent _SignalStartReceive = new AutoResetEvent(false);
/// <summary>
/// To implement variable time it takes until Receive returns
/// </summary>
private Random _RandomTime = new Random();
// Class Initializer
public Form()
{
backgroundWorker_Receive.WorkerReportsProgress = true;
backgroundWorker_Receive.RunWorkerAsync();
return;
}
/// <summary>
/// User presses this button when he is ready to Receive the next
/// data packet
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_ReceiveNext_Click(object sender, EventArgs e)
{
checkBox_Receive.Checked = true;
textBox_ReceivedContent.Text = "";
_SignalStartReceive.Set();
return;
}
/// <summary>
/// User presses this button when he is ready to Receive the next
/// data packet
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button_ReceiveNext_Click(object sender, EventArgs e)
{
checkBox_Receive.Checked = true;
textBox_ReceivedContent.Text = "";
_SignalStartReceive.Set();
return;
}
/// <summary>
/// This is the worker thread, running in the background
/// while the UI stays responsive
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void backgroundWorker_Receive_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
while (true)
{
// blocking: wait for button click
_SignalStartReceive.WaitOne();
// blocking: wait for datagram over network
#if true //temporary code to simulate UdpClient.Receive()
DateTime StartTime = DateTime.Now;
int RandomTimeMs = 2000 + 30 * _RandomTime.Next(100);
Thread.Sleep(RandomTimeMs);
_ReceivedDatagram = string.Format("UDP data ... {0} ms", (DateTime.Now - StartTime).TotalMilliseconds);
#else
something with UdpClient.Receive();
#endif
// succeeded:
worker.ReportProgress(0);//fire the event: Receive_ProgressChanged (argument does not matter)
}
//return; //unreachable, but would fire the Completed event
}
/// <summary>
/// Handler for the ReportProgress() call by the BackgroundWorker Thread
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void backgroundWorker_Receive_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
textBox_ReceivedContent.Text = _ReceivedDatagram;
checkBox_Receive.Checked = false;
return;
}