473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1884
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.