1.

Solve : creating threads in C#?

Answer»

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

Code: [SELECT]Thread firstThread = new Thread (new ThreadStart (Method1));
In other words, what is happening at each stage here:

Code: [Select]new ThreadStart (Method1)
Code: [Select]new Thread (new ThreadStart (Method1))
Code: [Select]Thread firstThread = new Thread (new ThreadStart (Method1));
Thanks for any help you can give.Is this homework?No, this is not homework. I'm trying to get back into an industry I was in years ago. I used to program in C before .Net existed.the keep programming in C... it's still a prominent language.

C#... well, if you know C there's no need to learn C#.If it's all the same to you, I'll set my own career path, thank you very much.

What a friendly, helpful, reasonable response. Quote from: BobLewiston on July 20, 2009, 11:52:25 AM

If it's all the same to you, I'll set my own career path, thank you very much.

What a friendly, helpful, reasonable response.

heh, on the other hand, C# would be a forward thinking investment... (assuming that .NET manages to take off, which wouldn't be an unfair assumption).

In either case... that wasn't relevant... my response I mean... to your question My apologies.

Code: [Select]Thread firstThread = new Thread (new ThreadStart (Method1));

As I'm sure your aware, unlike C, C++ and C# deal with Objects. Quite a departure from the Structured Programming of C and Pascal.

This line, as you know, creates a new thread. the "new" keyword is used to create a new thread. However, in order to create a new thread, you must pass in a "ThreadStart" object to the Thread's constructor, a special function that is called when you create an instance of the class. In this case, a new ThreadStart Object is created right in the parameter list to the Thread Constructor.

But oh Dear! the ThreadStart() Constructor also needs a bit of information- the METHOD that will comprise this thread. In your example, this is the function "method1".

If I am CORRECT, this code would create the thread- but would not START it, the firstThread object can of course be told to start,suspend, terminate, etc via it's methods.








Discussion

No Comment Found