C# 线程上的文件系统监视程序
谢谢你在这方面的帮助 我正在尝试编写一个小的FileWatcher应用程序,它监视一个本地目录,并将任何更改复制到另一个本地目录。我在.Net中使用了FileSystemWatcher类,在我的btnStart上单击我运行了四个线程,每个线程都有自己的FileSystemWatcher实例,监视不同的更改类型。因此,我首先要查找的是创建的事件C# 线程上的文件系统监视程序,c#,filesystemwatcher,C#,Filesystemwatcher,谢谢你在这方面的帮助 我正在尝试编写一个小的FileWatcher应用程序,它监视一个本地目录,并将任何更改复制到另一个本地目录。我在.Net中使用了FileSystemWatcher类,在我的btnStart上单击我运行了四个线程,每个线程都有自己的FileSystemWatcher实例,监视不同的更改类型。因此,我首先要查找的是创建的事件 new Thread(Created).Start(); 那么我有: void Created() { FileSystemWatcher Wa
new Thread(Created).Start();
那么我有:
void Created()
{
FileSystemWatcher Watcher2 = new FileSystemWatcher();
Watcher2.Path = txtBxDirToWatch.Text;
Watcher2.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.DirectoryName;
//watch all files in the path
Watcher2.Filter = "*.*";
//dont watch sub dir as default
Watcher2.IncludeSubdirectories = false;
if (chkBxIncSub.Checked)
{
Watcher2.IncludeSubdirectories = true;
}
Watcher2.Created += new FileSystemEventHandler(OnCreated);
Watcher2.EnableRaisingEvents = true;
}
我所要做的就是将任何更改复制到硬编码的本地路径,但我无法得到任何结果。这是我处理这件事的地方
public static void OnCreated(object source, FileSystemEventArgs e)
{
//combine new path into a string
string created = Path.Combine(@"C:\WatcherChanges", e.Name);
File.Create(created);
}
在您的代码线程中完成,然后GC收集并释放Watcher 不要将线程用于监视程序或“挂起”线程:
在您的代码线程中完成,然后GC收集并释放Watcher 不要将线程用于监视程序或“挂起”线程:
在告诉观察者启用引发事件后,线程立即退出。
Create
方法中没有任何东西可以保持线程运行。在Create
方法的末尾,FileSystemWatcher
超出范围,线程退出。它永远不会看到任何事件
有很多方法可以让线程等待。这里有一个简单的例子
public class Watcher
{
private ManualResetEvent resetEvent = new ManualResetEvent(false);
public ManualResetEvent ResetEvent { get { return resetEvent; }
void Created()
{
FileSystemWatcher Watcher2 = new FileSystemWatcher();
Watcher2.Path = txtBxDirToWatch.Text;
Watcher2.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.DirectoryName;
//watch all files in the path
Watcher2.Filter = "*.*";
//dont watch sub dir as default
Watcher2.IncludeSubdirectories = false;
if (chkBxIncSub.Checked)
{
Watcher2.IncludeSubdirectories = true;
}
Watcher2.Created += new FileSystemEventHandler(OnCreated);
Watcher2.EnableRaisingEvents = true;
resetEvent.WaitOne();
}
然后将调用Thread.Start
的方法更改为
Watcher watcher = new Watcher();
new Thread(watcher.Created).Start();
当你不想再看的时候
watcher.ResetEvent.Set();
你看过吗?它将为您执行此操作。在您告诉观察者启用引发事件后,您的线程将立即退出。
Create
方法中没有任何东西可以保持线程运行。在Create
方法的末尾,FileSystemWatcher
超出范围,线程退出。它永远不会看到任何事件
有很多方法可以让线程等待。这里有一个简单的例子
public class Watcher
{
private ManualResetEvent resetEvent = new ManualResetEvent(false);
public ManualResetEvent ResetEvent { get { return resetEvent; }
void Created()
{
FileSystemWatcher Watcher2 = new FileSystemWatcher();
Watcher2.Path = txtBxDirToWatch.Text;
Watcher2.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.DirectoryName;
//watch all files in the path
Watcher2.Filter = "*.*";
//dont watch sub dir as default
Watcher2.IncludeSubdirectories = false;
if (chkBxIncSub.Checked)
{
Watcher2.IncludeSubdirectories = true;
}
Watcher2.Created += new FileSystemEventHandler(OnCreated);
Watcher2.EnableRaisingEvents = true;
resetEvent.WaitOne();
}
然后将调用Thread.Start
的方法更改为
Watcher watcher = new Watcher();
new Thread(watcher.Created).Start();
当你不想再看的时候
watcher.ResetEvent.Set();
你看过吗?它将为您执行此操作。没有理由在单独的线程中创建观察程序 当您监视的目录中发生某些事情时,它们将通过线程返回给您。当他们这样做时-如果线程池线程与UI无关,那么您就在线程池线程上执行您的工作。如果是-您必须使用它的
是的,正如其他人已经说过的,不要让你的观察者得到GC'd。没有理由在单独的线程中创建观察者 当您监视的目录中发生某些事情时,它们将通过线程返回给您。当他们这样做时-如果线程池线程与UI无关,那么您就在线程池线程上执行您的工作。如果是-您必须使用它的
是的,正如其他人已经说过的,不要让你的观察者得到GC'd。你试过调试它吗?你的OnCreated是否会被点击然后你创建文件?你确定e.Name是一个文件名,而不是一个完整的文件路径吗?检查是否创建了这个新线程,因为你正在从新线程访问UI控件。我相信它正在抛出异常,而您没有看到它。您是否尝试调试它?你的OnCreated是否会被点击然后你创建文件?你确定e.Name是一个文件名,而不是一个完整的文件路径吗?检查是否创建了这个新线程,因为你正在从新线程访问UI控件。我相信它正在抛出异常,而您没有看到它。