C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
creating threads in C# Posted by BobLewiston on 17 Jul 2009 at 11:28 AM
Can someone please break down a simple statement for a relative newbie?

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


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

new ThreadStart (Method1)


new Thread (new ThreadStart (Method1))


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


Thanks for any help you can give.
Report
Re: creating threads in C# Posted by DataDink on 18 Jul 2009 at 2:27 PM
Assuming you understand exactly what a thread is:

"new Thread()" simply creates a new thread in memory

a thread however without any code to execute is pretty useless and that is all the "ThreadStart" object is. It's just a delegate that you create to pass the thread a method from your code.

This way your method runs in its own thread - simutaneous to the rest of your code.

So after all that statement is done there - you just start the thread and whatever method you've passed it will start executing "in the background" as it were.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.