471,315 Members | 1,604 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,315 software developers and data experts.

Calling a method on a sperate thread

I want to call a method passing a parameter. I want to do this call on
a seperate thread. For example,

public class Foo
{
public static void FooSend(Message message)
{
// Start a seperate thread to call Send(message) method
// everytime someone calls this FooSend method
}

private static void Send(Message message)
{
// send the message
}
}

I know the threadstart delegate takes only methods that has no
arguments. But in my case I want to pass a parameter to the method
call.

Can someone help me here? Thanks.

Jan 11 '06 #1
3 1821
Actually, in .NET 2.0, there is a ParameterizedThreadStart method which
takes an object parameter that you can pass to it. It's not really needed,
since you can use anonymous delegates in C# 2.0 to get around that.

However, I'll start with .NET 1.1 and before first. In that case, you
want to create a class:

public class FooSendInvoker
{
// The parameter to pass to send.
private Message message;

public FooSendInvoker(Message message)
{
this.message = message;
}

// Called by the thread.
public void Invoke()
{
// Make the call.
Foo.FooSend(message);
}
}

Then, in your code, you would do this:

// Create the invoker, passing the message.
FooSendInvoker invoker = new FooSendInvoker(message);

// Create the thread.
Thread t = new Thread(new ThreadStart(invoker.Invoke));

// Run.
t.Start();

In .NET 2.0, you can declare a method like this:

public static void FooSendInvoker(object message)
{
// Cast to a message.
Message m = (Message) message;

// Make the call.
Foo.FooSend(m);
}

And then in your code, you can use the ParameterizedThreadStart
delegate, like so:

Thread t = new Thread(FooSendInvoker);

// Call, passing the message.
t.Start(message);

But, C# 2.0 also makes it much easier to call the delegate, since you
can do this:

Thread t = new Thread(
delegate()
{
// Just make the call.
FooSend(message);
});

// Run the thread.
t.Start();

The message variable will be passed along to the method on the thread
and executed.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<kp******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
I want to call a method passing a parameter. I want to do this call on
a seperate thread. For example,

public class Foo
{
public static void FooSend(Message message)
{
// Start a seperate thread to call Send(message) method
// everytime someone calls this FooSend method
}

private static void Send(Message message)
{
// send the message
}
}

I know the threadstart delegate takes only methods that has no
arguments. But in my case I want to pass a parameter to the method
call.

Can someone help me here? Thanks.

Jan 11 '06 #2
kplku...@gmail.com wrote:
I want to call a method passing a parameter. I want to do this call on
a seperate thread.


Have a look at
http://www.pobox.com/~skeet/csharp/t...rameters.shtml

Jon

Jan 11 '06 #3
Thanks Nicolas. That helped. I am kinda new to C# and .NET, need to
implement something real fast. Thanks again for the help.

Jan 11 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Nick Flandry | last post: by
6 posts views Thread by Pohihihi | last post: by
4 posts views Thread by Edwin Gomez | last post: by
7 posts views Thread by =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post: by
9 posts views Thread by Pubs | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.