472,958 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1869
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tony Liu | last post by:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
5
by: joeblast | last post by:
I have a Web service that gets the financial periods and hold a reference to a disconnected dataset built at initialization. Web methods work on the dataset inside the web service. Everything is...
6
by: Pohihihi | last post by:
When a thread calls a method (and/or that method calls another method), are they thread safe if the first method is controlled on access?
4
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
7
by: Pubs | last post by:
I am learning C sharp. I want to call a function (any simple function. E.g. : Never ending while loop or large For loop inside the function). I want to call this function from the main application...
7
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a C# logging assembly with a static constructor and methods that is called from another C# Assembly that is used as a COM interface for a VB6 Application. Ideally I need to build a file...
9
by: Pubs | last post by:
Hi all, I want to call a function with some intial parameters with in a thread. At the end of the function execution it should return a value to the caller. Caller is outside the thread. ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.