[英]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.