Wait for process to end async and then call a function within the main form

荒凉一梦 提交于 2019-12-02 11:51:38
Woodman

You should use Dispatcher.Invoke or Dispatcher.BeginInvoke inside NotePadHasEnded() method to switch to UI thread as you only allowed to access UI objects from UI thread.

Check this post for further details.

Google for SynchronizationContext. The error occures because you UI thread is not syncronized with the second thread that runs then the editor was closed. I found a few examples that describe how to implement the syncronization: It's All About the SynchronizationContext and ExecutionContext vs SynchronizationContext. Hope this helps you ;-)

The Exited event occurs in another thead, and you can only access UI controls in their own thread (which is called UI thread). Since you are using Windows Forms, you should use the Control.Invoke method:

editor.Exited += delegate
{
    node.TreeView.Invoke(new Action<TreeNode>(NotePadHasEnded), node);
};

Also change the return type of the NotePadHasEnded to void.

node.TreeView is used to access the Invoke method. You can use any UI control. If the code resides in a form, you can use this instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!