在我的控制台应用程序中,同步线程内的事件变得困难。
using System;
using System.Threading;
using System.Windows.Forms;
namespace ConsoleApplication1
{
public class Example
{
private static Button _button;
private static readonly EventWaitHandle Ewh = new EventWaitHandle(false, EventResetMode.ManualReset);
private static readonly EventWaitHandle Btn = new EventWaitHandle(false, EventResetMode.ManualReset);
[STAThread]
public static void Main()
{
_button = new Button();
_button.Click += _button_Click;
for (int i = 0; i <= 0x4; i++)
{
var t = new Thread(ThreadProc);
t.Start(i);
}
Console.WriteLine("Press ENTER to release a waiting thread.");
Console.ReadLine();
Ewh.Set();
Console.ReadLine();
}
private static void _button_Click(object sender, EventArgs e)
{
Console.WriteLine(new Random().Next(0x1388));
Thread.Sleep(10);
Btn.Set();
}
public static void ThreadProc(object data)
{
_button.PerformClick();
Btn.WaitOne();
Btn.Reset();
Console.WriteLine("Thread {0} blocks.", data);
Ewh.WaitOne();
Console.WriteLine("Thread {0} exits.", data);
}
}
}
应用程序将结果作为一些随机数后跟线程块,并在控制台上打印出信号EventWaithandle数据线程之后。
目标是按照
之类的方式打印数据*随机数据
线程块
随机数据
线程块
...
线程退出
... *
示例输出
1234
线程2块
2345
线程0块
3456
线程1块
...
线程1退出
线程4退出
...
如何以这种方式同步线程内的线程和事件。
答案 0 :(得分:0)
使用任务计划程序和同步上下文。在主线程中创建同步上下文,并将其传递给要在主线程中执行的任务。
答案 1 :(得分:0)
这是我的代码。它为Btn使用AutoResetEvent。此外,我看到您正在创建Button并从后台线程访问它。我删除了这段代码,而是直接调用了ButtonClick方法。以下是生成的代码:
using System;
using System.Threading;
using System.Windows.Forms;
namespace ConsoleApplication1
{
public class Example
{
private static Button _button;
private static readonly EventWaitHandle Ewh = new EventWaitHandle(true, EventResetMode.ManualReset);
private static readonly EventWaitHandle Btn = new EventWaitHandle(true, EventResetMode.AutoReset);
[STAThread]
public static void Main()
{
Ewh.Reset();
_button = new Button();
_button.Click += _button_Click;
for (int i = 0; i <= 0x4; i++)
new Thread(ThreadProc).Start(i);
Console.WriteLine("Press ENTER to release a waiting thread.");
Console.ReadLine();
Ewh.Set();
Console.ReadLine();
}
private static void _button_Click(object sender, EventArgs e)
{
Console.WriteLine(new Random().Next(0x1388));
Thread.Sleep(10);
}
public static void ThreadProc(object data)
{
Btn.WaitOne();
Console.WriteLine("Thread {0} blocks.", data);
_button.PerformClick();
Btn.Set();
Ewh.WaitOne();
Console.WriteLine("Thread {0} exits.", data);
}
}
}