creating threads in C#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 80
Reputation: BobLewiston is an unknown quantity at this point 
Solved Threads: 0
BobLewiston BobLewiston is offline Offline
Junior Poster in Training

creating threads in C#

 
0
  #1
Jul 17th, 2009
Can someone please break down a simple statement for a relative newbie?

  1. Thread firstThread = new Thread (new ThreadStart (Method1));

In other words, what is happening at each stage here:

  1. new ThreadStart (Method1)

  1. new Thread (new ThreadStart (Method1))

  1. Thread firstThread = new Thread (new ThreadStart (Method1));

Thanks for any help you can give.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 64
Reputation: lighthead is on a distinguished road 
Solved Threads: 15
lighthead's Avatar
lighthead lighthead is offline Offline
Junior Poster in Training

Re: creating threads in C#

 
0
  #2
Jul 17th, 2009
Thread is a built in class in .NET to allow threading.
firstThread is a new thread you are creating.
  1. new Thread (new ThreadStart (Method1))
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.
  1. new ThreadStart (Method1)
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.
  1. firstThread.Start();
You might want to check out this.
Everybody Lies.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: creating threads in C#

 
0
  #3
Jul 17th, 2009
The new ThreadStart(Method1) simply encapsulates a method that will be called when the thread is started. The new Thread(new ThreadStart(Method1)) creates a new Thread object that can be used to reference the thread and start running the thread, with the starting point Method1. Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: creating threads in C#

 
2
  #4
Jul 17th, 2009
read the attached.
Attached Files
File Type: pdf threading.pdf (464.4 KB, 43 views)
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 80
Reputation: BobLewiston is an unknown quantity at this point 
Solved Threads: 0
BobLewiston BobLewiston is offline Offline
Junior Poster in Training

Re: creating threads in C#

 
0
  #5
Jul 17th, 2009
Thanks a lot, guys.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 124
Reputation: thewebhostingdi has a little shameless behaviour in the past 
Solved Threads: 11
thewebhostingdi thewebhostingdi is offline Offline
Junior Poster

Re: creating threads in C#

 
-1
  #6
Jul 18th, 2009
  1. Thread t = new Thread(new ThreadStart(this.YourFunction));
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1
Reputation: cq2535 is an unknown quantity at this point 
Solved Threads: 0
cq2535 cq2535 is offline Offline
Newbie Poster

Re: creating threads in C#

 
1
  #7
Jul 22nd, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC