This is not related to multithreading itself, it is related to UI invocation mechanism. In the
System.Windows.Forms
library, it is based on Windows message queue. Naturally, you can put anything to a queue of some windowed control only if this control already has a window handle. In your code, the instance
ctrl
is just created through its constructor. Naturally, it has no a window handle. Look at what you are doing: you are calling
ctrl.Invoke
on a control distance which is not even a part of the UI yet.
So, you are trying to use
Control.Invoke
well too early :-).
Resolution: replace
ctrl.Invoke
with
Form1.Invoke
. It is absolutely not important which control is added for invocation, it is any control which already participates in the UI of the thread you want to invoke the method to.
And please never use names like
Form1
as well as any other auto-generated names. Such names violate Microsoft naming conventions. Always renames such object using some semantically meaningful names.
Background:
Please see my past answers on the topic:
Problem with Treeview Scanner And MD5[
^],
Control.Invoke() vs. Control.BeginInvoke()[
^].
—SA