使用线程处理事件

时间:2015-11-06 00:57:08

标签: c# multithreading events

总体情况是从body { background-color:#d6d6d6; } .container { position: relative; margin: 200px auto; background-color:red; width:478px; height:200px; } img { float:left; width:239px; height:200px; } #facial { position:absolute; right: 0; width:239px; height:200px; background-color:#008aaf; } #facial h1, #facial h2 { text-align:center; margin-top:30px; color:#FFFFFF; } 打印Thread t到某个值,然后暂停执行,具体取决于执行另一个线程1的{​​{1}}。 signal (ManualResetEvent)完成后,s。因此,s恢复执行。

sets the signal

程序在前半部分按预期工作,直到T class A { int key; int temp; public A(int x) { key = x; } public EventHandler eventHandler; private void onKeyReached(object sender, EventArgs e) { eventHandler(this, e); } public int Temp { get { return temp; } set { if (value == temp) return; else if (temp == key) onKeyReached(this, EventArgs.Empty); else temp = value; } } } class Test { static A a = new A(500); //signal to pause or resume threads static ManualResetEvent signal = new ManualResetEvent(true); static void Main() { a.eventHandler += WorkOnKeyReached; Thread t = new Thread(F); t.Start(); } static void F() { for (int i = 0; i < 1000; i++) { a.Temp = i; //when a.Temp == a.Key, a.OnkeyReached() is fired. signal.WaitOne(); Console.WriteLine(a.Temp); } } static void WorkOnKeyReached(object sender, EventArgs e) { signal.Reset(); Thread s = new Thread(() => { Console.WriteLine("Doing something else..."); Thread.Sleep(3000); Console.WriteLine("Done.."); signal.Set(); }); s.Start(); } } t signal暂停reset。 在WorkOnKeyReached完成后,我WorkOnKeyReached set期待signal继续。

输出中:

t

任何人都可以帮助解释这种现象吗?我只是在玩Threads和其他东西。

1 个答案:

答案 0 :(得分:0)

    public int Temp
{
    get { return temp; }
    set
    {
        if (value == temp) return;
        else if (temp == key)
            onKeyReached(this, EventArgs.Empty);
       //else
            temp = value;
    }
}

基本上取出其他或者永远不会超过500