By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,056 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,056 IT Pros & Developers. It's quick & easy.

Threads and Delegates

P: n/a
If I have a procedure that takes an int as a parameter, how do I launch it on
a separate thread and send the parameter it needs?

Thanks.

Jerry
Sep 26 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

You can do it like this:
private void TestThread()
{
ParameterizedThreadStart threadStart = new
ParameterizedThreadStart(MyFunction);
Thread myThread = new Thread(threadStart);
myThread.Start(5); // Here 5 is the integer argument to be
passed on MyFunction()
}

private static void MyFunction(object i /*cannot declare it as
of type in, as ParameterizedThreadStart object only accepts functions
with object argument */)
{
int myInt = (int) i;
}

Cheers
Dips
rlrcstr wrote:
If I have a procedure that takes an int as a parameter, how do I launch it on
a separate thread and send the parameter it needs?

Thanks.

Jerry
Sep 26 '06 #2

P: n/a
Oops one correction!!

MyFunction(..) method need not to be a static method.
Dipankar wrote:
Hi,

You can do it like this:
private void TestThread()
{
ParameterizedThreadStart threadStart = new
ParameterizedThreadStart(MyFunction);
Thread myThread = new Thread(threadStart);
myThread.Start(5); // Here 5 is the integer argument to be
passed on MyFunction()
}

private static void MyFunction(object i /*cannot declare it as
of type in, as ParameterizedThreadStart object only accepts functions
with object argument */)
{
int myInt = (int) i;
}

Cheers
Dips
rlrcstr wrote:
If I have a procedure that takes an int as a parameter, how do I launch it on
a separate thread and send the parameter it needs?

Thanks.

Jerry
Sep 26 '06 #3

P: n/a
rlrcstr <rl*****@discussions.microsoft.comwrote:
If I have a procedure that takes an int as a parameter, how do I launch it on
a separate thread and send the parameter it needs?
See http://www.pobox.com/~skeet/csharp/t...rameters.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #4

P: n/a
Dipankar,

Use delegate's BeginInvoke method. It will run the method pointed by the
delegate in a thread from the thread pool. The begin invoke method will have
parameters to pass value(s) according to the delegate declaration.

delegate void FooDelegate(int i);

void Foo(int a)
{
Console.WriteLine(a);
}
Executing the Foo method in a worker thread:

new FooDelegate(Foo).BeginInvoke(10, null, null);
--
HTH
Stoitcho Goutsev (100)

"Dipankar" <di**********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Oops one correction!!

MyFunction(..) method need not to be a static method.
Dipankar wrote:
>Hi,

You can do it like this:
private void TestThread()
{
ParameterizedThreadStart threadStart = new
ParameterizedThreadStart(MyFunction);
Thread myThread = new Thread(threadStart);
myThread.Start(5); // Here 5 is the integer argument to be
passed on MyFunction()
}

private static void MyFunction(object i /*cannot declare it as
of type in, as ParameterizedThreadStart object only accepts functions
with object argument */)
{
int myInt = (int) i;
}

Cheers
Dips
rlrcstr wrote:
If I have a procedure that takes an int as a parameter, how do I launch
it on
a separate thread and send the parameter it needs?

Thanks.

Jerry

Sep 26 '06 #5

P: n/a
Great info. Thanks guys.

"Jon Skeet [C# MVP]" wrote:
rlrcstr <rl*****@discussions.microsoft.comwrote:
If I have a procedure that takes an int as a parameter, how do I launch it on
a separate thread and send the parameter it needs?

See http://www.pobox.com/~skeet/csharp/t...rameters.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.