This is not how threading works. It's not "changing method". You need to create or obtain a thread and call your method from the thread. The thread will work when you start it. But the major concern is the thread synchronization, in particular, sharing objects between threads. This should be reduced to necessary minimum, but in most cases unavoidable. It includes the
lock
statement, thread synchronization objects (
"primitives"), invocation delegates to the UI thread (
Invoke
and
BegingInvoke
methods of
System.Windows.Threading.Dispatcher
or
System.Windows.Forms.Control
and more. I think you need to learn it all by yourself, as it would be a bit too much for a single
Quick Answer.
Please see
System.Threading.Thread
:
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx[
^].
To create or obtain an additional thread, you can
- Create a thread instance with its constructor:
http://msdn.microsoft.com/en-us/library/xx3ezzs2.aspx[^].
I want to discourage using ParametrizedThreadStart
as it involves type cast. Much better approach involves creating a thread wrapper, where "this" parameter is implicitly used to access the whole wrapper instance. It also allows to transparently encapsulate thread synchronization and more. Please see my past answers:
How to pass ref parameter to the thread[^],
Change parameters of thread (producer) after it is started[^],
MultiThreading in C#[^]. - You can obtain a thread from the thread pool. Please see:
http://en.wikipedia.org/wiki/Thread_pool_pattern[^],
http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx[^]. - Also, you can use the class
System.ComponentModel.BackgroundWorker
:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^]
As the alternative to threading, you can consider using .NET Parallel Extensions, which is based on threading, but threads are not used explicitly and the solutions may appear single-threaded, transparently.
Please see:
http://en.wikipedia.org/wiki/Parallel_Extensions[
^],
http://msdn.microsoft.com/en-us/library/system.threading.tasks.aspx[
^],
http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.aspx[
^].
See also my past answers 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