更改了c#追加表单



我试图追加文本框每次有一个变化在我的日志文本文件在c#

下面是我的代码,但我似乎不能这样做。它一直告诉我从另一个线程访问主形式的文本框组件c#

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

最新更新