| | |
creating threads in C#
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 80
Reputation:
Solved Threads: 0
Can someone please break down a simple statement for a relative newbie?
In other words, what is happening at each stage here:
Thanks for any help you can give.
C# Syntax (Toggle Plain Text)
Thread firstThread = new Thread (new ThreadStart (Method1));
In other words, what is happening at each stage here:
C# Syntax (Toggle Plain Text)
new ThreadStart (Method1)
C# Syntax (Toggle Plain Text)
new Thread (new ThreadStart (Method1))
C# Syntax (Toggle Plain Text)
Thread firstThread = new Thread (new ThreadStart (Method1));
Thanks for any help you can give.
Thread is a built in class in .NET to allow threading.
firstThread is a new thread you are creating.
Creates a new instance of the thread.The code that is to be executed is method 'Method1'. The new thread cannot directly run this method so we have to create a delegate.
ThreadStart is that delegate, we create a new instance of it and supply it to the thread.
After this you have to call Start to run the thread.
You might want to check out this.
firstThread is a new thread you are creating.
C# Syntax (Toggle Plain Text)
new Thread (new ThreadStart (Method1))
C# Syntax (Toggle Plain Text)
new ThreadStart (Method1)
After this you have to call Start to run the thread.
C# Syntax (Toggle Plain Text)
firstThread.Start();
Everybody Lies.
•
•
Join Date: Jun 2009
Posts: 124
Reputation:
Solved Threads: 11
C# Syntax (Toggle Plain Text)
Thread t = new Thread(new ThreadStart(this.YourFunction)); t.Start();
Above code will start the execution of function YourFunction(). You can consider that function as the entry point of a new process. In the ThreadStart this.YourFunction is a delegate that has a pointer to the function to execute.
•
•
Join Date: Jul 2007
Posts: 1
Reputation:
Solved Threads: 0
Somethings not mention, and should be made clear.
ThreadStart is a delegate. Delegates are used as an "alias" which points to some other function. They can be re-used for many functions not just the one.
The ThreadStart in particular is a NO ARGUMENT delegate and so when you create the referring function "Method1" it should not contain parameters.
The other delegate you can use is the ParameterizedThreadStart delegate which accepts only ONE ARGUMENT of type object. So when you create the referring function "MethodOneParam" it should contain 1 parameter of type object.
You cannot change the delegate signatures since these two are part of the Framework. But.. you most certainly can create your OWN delegates of multivariable type safe delegates.
Another important note is that using the Thread class in a WinForms/WPF application, you cannot refer to the underlying controls unless that Thread performs a synchronized postback through the SychronizationContext/Dispatcher as UI elements cannot be manipulated or referenced in other threads besides the UI's.
ThreadStart is a delegate. Delegates are used as an "alias" which points to some other function. They can be re-used for many functions not just the one.
The ThreadStart in particular is a NO ARGUMENT delegate and so when you create the referring function "Method1" it should not contain parameters.
The other delegate you can use is the ParameterizedThreadStart delegate which accepts only ONE ARGUMENT of type object. So when you create the referring function "MethodOneParam" it should contain 1 parameter of type object.
You cannot change the delegate signatures since these two are part of the Framework. But.. you most certainly can create your OWN delegates of multivariable type safe delegates.
Another important note is that using the Thread class in a WinForms/WPF application, you cannot refer to the underlying controls unless that Thread performs a synchronized postback through the SychronizationContext/Dispatcher as UI elements cannot be manipulated or referenced in other threads besides the UI's.
![]() |
Similar Threads
- Send data on a serial port (C++)
- Threads in C++ (C++)
- Parents/Children - Processes/Threads (C++)
- Problem with socket (C++)
- Mutlitthreaded programming (C)
- multithreading & destructor call. (C++)
- pthreads scheduling problem? (C)
Other Threads in the C# Forum
- Previous Thread: search button and
- Next Thread: How to block some programs?
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client color combobox control conversion csharp custom data database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox serialization server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update upload usercontrol users validate validation visualstudio webbrowser windows winforms wpf xml






