如何等待第二个线程上的事件 [英] How to wait on events on a second thread

查看:108
本文介绍了如何等待第二个线程上的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的代码,在一个名为Worker的类中:

  FileSystemWatcher watcher = new FileSystemWatcher(); 
public void Run()
{
watcher.Path = @c:\queue;
watcher.Created + = new FileSystemEventHandler(watcher_Created);
watcher.EnableRaisingEvents = true;
}

现在这里是我想做的工作,但是这不是

  Worker worker = new Worker(); 
线程=新线程(worker.Run);
thread.Start();
Console.ReadLine(); //其他更有趣的东西会去这里。

原因是Run方法结束而不是进入某种事件循环。如何获得事件循环。 (我想我在寻找像Application.Run这样的东西)

解决方案

没有测试,但是你的事件你想要在线程上运行,看起来像这样:

  private bool running = true; 
private AutoResetEvent waiter = new AutoResetEvent(false);

public void Run()
{
FileSystemWatcher watcher = new FileSystemWatcher(C:\\);
FileSystemEventArgs changes = null;

watcher.Changed + =
(object sender,FileSystemEventArgs e)=> {
changes = e;
服务员。 Set();
};

watcher.EnableRaisingEvents = true;

while(running)
{
waiter.WaitOne ;
if(!running)break;

Console.WriteLine(Path:{0},Type:{1},
changes.FullPath,changes.ChangeType) ;
}

Console.WriteLine(Threa d完成);
}

public void Stop()
{
running = false;
waiter.Set();
}

您可以将此代码放在一个类中,并在单独的线程上运行如果你希望)。如果你想让你的主线程等待线程创建完成(尽管为什么在这种情况下打扰创建一个线程),那么你可以使用 Thread.Join 方法


Consider the following code in a class called Worker

    FileSystemWatcher watcher = new FileSystemWatcher();
    public void Run()
    {
        watcher.Path = @"c:\queue";
        watcher.Created += new FileSystemEventHandler(watcher_Created);
        watcher.EnableRaisingEvents = true;
    }

Now here's what I would like to do wih the Worker, but obviouly this doesn't fly.

        Worker worker = new Worker();
        Thread thread = new Thread(worker.Run);
        thread.Start();
        Console.ReadLine(); // something else more interesting would go here.

The reason being that the Run method ends rather than going into some sort of event loop. How do I get an event loop going. (I think I"m looking for something like Application.Run)

解决方案

Not tested, but your "event" loop, if you're wanting it to run on the thread, would look something like this:

private bool running = true;
private AutoResetEvent waiter = new AutoResetEvent(false);

public void Run()
{
    FileSystemWatcher watcher = new FileSystemWatcher("C:\\");
    FileSystemEventArgs changes = null;

    watcher.Changed +=
        (object sender, FileSystemEventArgs e) => {
            changes = e;
            waiter.Set();
        };

    watcher.EnableRaisingEvents = true;

    while (running)
    {
        waiter.WaitOne();
        if (!running) break;

        Console.WriteLine("Path: {0}, Type: {1}",
                changes.FullPath, changes.ChangeType);
    }

    Console.WriteLine("Thread complete");
}

public void Stop()
{
    running = false;
    waiter.Set();
}

You would place this code in a class and run it on a separate thread (if you wish). If you want to have your main thread wait on the thread you create finishing (although why bother creating a thread in that case?) then you can use the Thread.Join method.

这篇关于如何等待第二个线程上的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆