I am trying to append textbox everytime there is a change in my log text file in c#.
Below is my code but I cant seem to do it. it keep tells me that access to textbox component in main form from another thread 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 delivers events on a different thread, you will have to marshall the AppendText calls to the UI dispatcher:
textBox1.Dispatcher.BeginInvoke(new Action(() =>
{
textBox1.AppendText("hello ah");
}));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.