473,394 Members | 1,797 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,394 software developers and data experts.

Threads with signatures different of ThreadStart

Hi,

ThreadStart doesn't take any parameters. How can I create threads that take
parameters? O:-)

Thanks
Nov 16 '05 #1
4 1376
A common technique is to create a class wich holds the parameters and then
let the thread work on that.

ie;

<code>

public class WorkerClass
{
public int startInt;
public string startString;
Thread workerThread;

public WorkerClass(int StartInt, string StartString)
{
this.startInt = StartInt;
this.startString = StartString;
}

public void Start()
{
workerThread = new Thread(new ThreadStart(this.Process);
workerThread.Start();
}

public void Process()
{
// use variables;
}
}

class StarterApp
{
static void Main()
{
WorkerClass work = new WorkerClass(1, "fortytwo");
work.Start();
}
}

</code>

--
Patrik Löwendahl [C# MVP]
www.cshrp.net - "Elegant code by witty programmers"

"Fernando Rodríguez" <fe*******************@fernando-rodriguez.com> wrote in
message news:3o********************************@4ax.com...
Hi,

ThreadStart doesn't take any parameters. How can I create threads that
take
parameters? O:-)

Thanks

Nov 16 '05 #2
Hi Fernando.,
the easiest thing you can do is something like the following:

class Main{
[STAThread]
static void Main(string[] args)
{
ThreadStartWrap ts = new ThreadStartWrap("some value that might not be a
string ...");
Thread t = new Thread(new System.Threading.ThreadStart(ts.StartPoint));
t.Start();
}
}

class ThreadStartWrap
{
object _myParam;
public ThreadStartWrap(object myParam)
{
this._myParam = myParam;
}
public void StartPoint()
{
//use input param here
Console.WriteLine(_myParam);
}
}

It depends on the case whether you need something more complicated - like
putting in some eventhandling or some thread synch logic, but that's the
general idea.

Cheers,
Branimir

--
Branimir Giurov
C# MVP, MCSD.NET, MCDBA, MCT
www.sofiadev.org
"Fernando Rodríguez" <fe*******************@fernando-rodriguez.com> wrote in
message news:3o********************************@4ax.com...
Hi,

ThreadStart doesn't take any parameters. How can I create threads that take parameters? O:-)

Thanks

Nov 16 '05 #3
If you want a threadpool thread you could do this:

private delegate void MyDelegate(string text);
private void button6_Click(object sender, System.EventArgs e)
{
// Fire a simple method on the thread pool with no args and void return
using a system defined delegate.
new System.Windows.Forms.MethodInvoker(DoThis).BeginIn voke(null, null);

// Fire a custom delegate on the thread pool with user defined delegate.
new MyDelegate(DoThat).BeginInvoke("Delegates are fun.", null, null);
}

private void DoThis()
{
Console.WriteLine("Did this.");
Console.WriteLine("Is Thread Pool?:" +
Thread.CurrentThread.IsThreadPoolThread);
}

private void DoThat(string text)
{
// Also call EndInvoke.
Console.WriteLine("Did that:"+text);
Console.WriteLine("Is Thread Pool?:" +
Thread.CurrentThread.IsThreadPoolThread);
}

--
William Stacey, MVP

"Fernando Rodríguez" <fe*******************@fernando-rodriguez.com> wrote in
message news:3o********************************@4ax.com...
Hi,

ThreadStart doesn't take any parameters. How can I create threads that take parameters? O:-)

Thanks


Nov 16 '05 #4
Fernando Rodríguez <fe*******************@fernando-rodriguez.com>
wrote:
ThreadStart doesn't take any parameters. How can I create threads that take
parameters? O:-)


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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Leonardo Hyppolito | last post by:
Hello! I am trying to implement a program that uses Threads. I chose the producers and consumers scenario. The producers put a "product" (which is an int number) in a shared storage place of one...
2
by: karl | last post by:
I have a windows service that kicks off a 'monitor' thread which in turn kicks off 4 additional threads. These 4 threads basically are listen on a designated socket and report back any errors...
7
by: Mr. Mountain | last post by:
In the following code I simulate work being done on different threads by sleeping a couple methods for about 40 ms. However, some of these methods that should finish in about 40 -80 ms take as long...
3
by: EAI | last post by:
Hello All, How to abort or make sure the child threads are aborted before aborting the parent thread? Thanks
1
by: Bryan Ax | last post by:
We have some code that's having problems when multiple applications running as different identities try to hit this code - the code creates a Mutex, and when the second identity tries to access it,...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
6
by: Dave | last post by:
I have a service that has 6 different threads. Each thread has a timer on it that elapses at about the same time (if not the same time). When the timer elapses I am trying to log a message by...
15
by: Bryce K. Nielsen | last post by:
I have an object that starts a thread to do a "process". One of the steps inside this thread launches 12 other threads via a Delegate.BeginInvoke to process. After these 12 threads are launched,...
0
by: simplediscourse | last post by:
i'm trying to demonstrate timeslicing among equal high priority threads without putting threads to sleep. to do this i'm trying to run a timer while executing a very long loop sequence in order to...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.