There are some mistakes here, all about threading.
The situation with exception is also related to threads. The handler of the event
ProcessExited
was called from the thread other than UI thread. You cannot call any UI properties or methods from this thread. Instead, you should use the method
Invoke
or
BeginInvoke
of
Dispatcher
or
Control
. It does not matter what instance of
Control
to use; it could be your form, for example. If you are not sure if some code is running in UI thread or not, you can check if via
Control.InvokeRequired
.
You can find all on how to use invocation and how it works here:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
As a rule of thumb, never use
DoEvents
. This is a common mistake. If you want to do some processing which takes time, don't do in in the UI thread, use a special thread for this purpose.
See my past answers on how to use threading with Forms. This is a collection of references to my past Answers, good enough to cover your topics:
How to get a keydown event to operate on a different thread in vb.net[
^].
—SA