943,741 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1626
  • C# RSS
Jul 17th, 2009
0

creating threads in C#

Expand Post »
Can someone please break down a simple statement for a relative newbie?

C# Syntax (Toggle Plain Text)
  1. Thread firstThread = new Thread (new ThreadStart (Method1));

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

C# Syntax (Toggle Plain Text)
  1. new ThreadStart (Method1)

C# Syntax (Toggle Plain Text)
  1. new Thread (new ThreadStart (Method1))

C# Syntax (Toggle Plain Text)
  1. Thread firstThread = new Thread (new ThreadStart (Method1));

Thanks for any help you can give.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
BobLewiston is offline Offline
105 posts
since Nov 2008
Jul 17th, 2009
0

Re: creating threads in C#

Thread is a built in class in .NET to allow threading.
firstThread is a new thread you are creating.
C# Syntax (Toggle Plain Text)
  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.
C# Syntax (Toggle Plain Text)
  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.
C# Syntax (Toggle Plain Text)
  1. firstThread.Start();
You might want to check out this.
Reputation Points: 72
Solved Threads: 15
Junior Poster in Training
lighthead is offline Offline
64 posts
since Jan 2008
Jul 17th, 2009
0

Re: creating threads in C#

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.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Jul 17th, 2009
2

Re: creating threads in C#

read the attached.
Attached Files
File Type: pdf threading.pdf (464.4 KB, 101 views)
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Jul 17th, 2009
0

Re: creating threads in C#

Thanks a lot, guys.
Reputation Points: 10
Solved Threads: 0
Junior Poster
BobLewiston is offline Offline
105 posts
since Nov 2008
Jul 18th, 2009
-1

Re: creating threads in C#

C# Syntax (Toggle Plain Text)
  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.
Reputation Points: 3
Solved Threads: 14
Junior Poster
thewebhostingdi is offline Offline
168 posts
since Jun 2009
Jul 22nd, 2009
1

Re: creating threads in C#

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.
Reputation Points: 15
Solved Threads: 0
Newbie Poster
cq2535 is offline Offline
1 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: search button and
Next Thread in C# Forum Timeline: How to block some programs?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC