2009-07-30 53 views
1

考虑一类下面的代码称为工人如何在事件等待第二个线程

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

现在,这里是我想要做的王氏工人什么,但obviouly这种不会飞。

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

原因是,Run方法结束,而不是进入某种事件循环。我如何获得一个事件循环。 (我想我“正在寻找像Application.Run)

回答

2

没有测试拉高,但你的“事件”的循环,如果你想它的线程上运行,会是这个样子:

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(); 
} 

你会把这段代码放在一个类中,并在单独的线程上运行它(如果你愿意的话)。如果你想让主线程等待你创建完成的线程(尽管为什么还要在这种情况下创建一个线程?),那么你可以使用Thread.Join方法。

0

BackgroundWorker的,与RunAsync()一起使用时,会在一个单独的线程中运行你的逻辑。

什么是你想实现你的工人说涉及一个事件循环?FileSystemWatcher已经基本上为你执行一个事件循环,所以目前还不清楚你想在这里实现什么。

+0

我不明白你的意思“FileSystemWatcher已经在执行一个事件循环”。一旦Run方法结束,线程的执行路径就完成了。线程然后停止,因此没有线程执行FileSystemWatcher事件。 – 2009-07-30 19:04:47

+0

watcher.EnableRaisingEvents = true;开始观看文件系统的对象,基本上启动“事件循环”。发生变化时,事件将被调用。检查这里的例子:http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.enableraisingevents.aspx – 2009-07-30 19:07:46

+0

我明白你现在正在尝试做什么。您不需要在单独的线程上执行Run()。一旦Run()完成,观察者将仍然存在并且正在监视文件系统。 – 2009-07-30 19:10:22

0

你可不要让Run()方法结束,直到应用程序准备好关闭?也许更像是:(抱歉,但我通过内存不搭配IDE,所以它可能不完美):

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

    try 
    { 
     while(true) 
      Thread.Sleep(); 
    } 
    catch(ThreadAbortedException) 
    { 
     return; 
    } 
} 

和亚军:

Worker worker = new Worker(); 
Thread thread = new Thread(worker.Run); 
thread.Start(); 
Console.ReadLine(); // something else more interesting would go here. 
thread.Abort(); // do when ready to close the app. 

关于第二个想法,你为什么要开始一个新的线程来做到这一点?为什么不干脆:

watcher.Path = @"C:\queue"; 
watcher.Created += new FileSystemEventHandler(watcher_Created); 
watcher.EnableRaisingEvents = true; 
Console.ReadLine(); // something else more interesting would go here. 

还是坚持你的FileSystemWatcher的一个变量的地方,都不会出门的范围(主要形式,如果一个WinForms应用程序上,或任何类有如果控制台应用程序的Main()方法)。

还是有其他的东西在这里没有显示?

1

MSDN

Worker worker = new Worker(); 
Thread thread = new ThreadStart(worker.Run); 
thread.Start(); 
// Not sure if you need this while 
while (!oThread.IsAlive); 
oThread.Join(); 
Console.ReadLine();