Here is how the problem looks:
You cannot call anything related to UI from non-UI thread. Instead, you need to use the method
Invoke
or
BeginInvoke
of
System.Windows.Threading.Dispatcher
(for both Forms or WPF) or
System.Windows.Forms.Control
(Forms only).
You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[
^],
Problem with Treeview Scanner And MD5[
^].
[EDIT]
Now, one of the problems is: you are creating the thread using its constructor on some click. This can makes number of running threads uncontrolled and pretty expensive. You can use better options: 1) do the same thing, but create extra thread in the very beginning of the run-time and throttle them by keeping them in a wait state using some thread synchronization primitives, typically
System.Threading.EventWaitHandle
; 2) use
System.Threading.ThreadPool
,
http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx[
^]; 3) use the class
System.ComponentModel.BackgroundWorker
,
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[
^].
[END EDIT]
See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[
^],
Control events not firing after enable disable + multithreading[
^].
—SA