Threads

B

Bonj

Can a C# Thread start on a non-static member function of a
specific instance of a class, like
System.Threading.Thread mythread ...
mythread.Start(new System.Threading.ThreadStart
(this.MyNonStaticFunc)


Or does it have to start on a static member function of
the class itself, like C++?
 
N

Nicholas Paldino [.NET/C# MVP]

Bonj,

It can start on a method of an instance. Basically, you need a
delegate, which is a type safe way of referring to a method. This method
can be an instance method, or a static method, it does not matter. As long
as the signature matches the ThreadStart delegate, then it can be used.

Hope this helps.
 
J

Jon Skeet [C# MVP]

Bonj said:
Can a C# Thread start on a non-static member function of a
specific instance of a class, like
System.Threading.Thread mythread ...
mythread.Start(new System.Threading.ThreadStart
(this.MyNonStaticFunc)

Absolutely. For example:

using System;
using System.Threading;

public class Test
{
int counter;

Test(int counter)
{
this.counter=counter;
}

void Count()
{
for (int i=0; i < counter; i++)
{
Console.WriteLine (i);
Thread.Sleep(100);
}
}

static void Main()
{
Test t1 = new Test (5);
Test t2 = new Test (10);

new Thread (new ThreadStart(t1.Count)).Start();
new Thread (new ThreadStart(t2.Count)).Start();
}
}
 
B

Bonj

That's what I want to do! thanks

-----Original Message-----


Absolutely. For example:

using System;
using System.Threading;

public class Test
{
int counter;

Test(int counter)
{
this.counter=counter;
}

void Count()
{
for (int i=0; i < counter; i++)
{
Console.WriteLine (i);
Thread.Sleep(100);
}
}

static void Main()
{
Test t1 = new Test (5);
Test t2 = new Test (10);

new Thread (new ThreadStart(t1.Count)).Start();
new Thread (new ThreadStart(t2.Count)).Start();
}
}

--
Jon Skeet - <[email protected]>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.
 
B

Bonj

This seems like one of the ways in which C# is actually
more powerful than C++ then, as C++ can only start a
thread on a global or static method - is it?

Are there any other nifty things like this in which C# is
more powerful than C++?


-----Original Message-----
Bonj,

It can start on a method of an instance. Basically, you need a
delegate, which is a type safe way of referring to a method. This method
can be an instance method, or a static method, it does not matter. As long
as the signature matches the ThreadStart delegate, then it can be used.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Can a C# Thread start on a non-static member function of a
specific instance of a class, like
System.Threading.Thread mythread ...
mythread.Start(new System.Threading.ThreadStart
(this.MyNonStaticFunc)


Or does it have to start on a static member function of
the class itself, like C++?


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top