![](/img/trans.png)
[英]c# FileSystemWatcher triggers twice when listening to OnChanged
[英]Onchanged c# appending form
每当c#中的日志文本文件发生更改时,我都尝试附加文本框。
下面是我的代码,但我似乎做不到。 它一直告诉我从另一个线程c#以主要形式访问textbox组件
public Form1()
{
InitializeComponent();
string currentPath = System.Environment.CurrentDirectory;
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = currentPath;
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
private void OnChanged(object source, FileSystemEventArgs e)
{
textBox1.AppendText("hello ah");
}
FileSystemWatcher在不同的线程上传递事件,您必须将AppendText调用编组到UI调度程序:
textBox1.Dispatcher.BeginInvoke(new Action(() =>
{
textBox1.AppendText("hello ah");
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.