Threading

G

Guest

hi,all

this is a stupid question.

I want to use thread
when I create a new ThreadStart,

ThreadStart thread = new System.ThreadThreading.ThreadStart(ThreadProc)

I want to pass some parameter to ThreadProc
how to do it?

thanks
 
F

Francois Bonin [C# MVP]

if all you need is to share some data with ThreadProc, you should declare a
class variable that will be assigned the data. (watch out for
synchronisation issues if you are going to be modifying the value in
question).

if you absolutely want to pass some parameters, you should declare a
delegate for the method you want to call and use the BeginInvoke method.
Like this:


public class example
{
private void mythreadmethod(string someparam)
{
//do whatever
}

public delegate void ParamMethod(string someParam);

public static void Main()
{
example ex = new example();
ParamMethod pm = new ParamMethod(ex.mythreadmethod);
string argToPass = "hello";
pm.BeginInvoke(argToPass, null, null);
}
}

This is a very simple example (for example, a lot can be said about the last
2 parameters of the BeginInvoke method ...) but it illustrates what you need

HTH
Cois
 

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