multithreading - Onchanged c# appending form -
i trying append textbox everytime there change in log text file in c#.
below code cant seem it. keep tells me access textbox component in main form 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 different thread, have marshall appendtext calls ui dispatcher:
textbox1.dispatcher.begininvoke(new action(() => { textbox1.appendtext("hello ah"); }));
Comments
Post a Comment