Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a new Thread to execute an Action<T>

The title pretty much says it. I have some methods that need to run on a new thread and since all the code before creating the thread is pretty much the same, I thought I would create a function that could take as a parameter the Action I need to invoke.

Problem is, I have not found how to tell the thread that it needs to execute the Action. Is that even possible? Here's a little sample code of what I'm trying to do.

private void ExecuteInBiggerStackThread(Action<Helper> action, Parameters parms)
{
    ParameterizedThreadStart operation = new ParameterizedThreadStart(action);// here's the mess
    Thread bigStackThread = new Thread(operation, 1024 * 1024);

    bigStackThread.Start(parms);
    bigStackThread.Join();
}

Regards,
seba

like image 626
sebagomez Avatar asked Jul 20 '10 14:07

sebagomez


People also ask

How do I start a new thread in C#?

Create New Thread [C#] First, create a new ThreadStart delegate. The delegate points to a method that will be executed by the new thread. Pass this delegate as a parameter when creating a new Thread instance. Finally, call the Thread.

How many ways we can create thread in C#?

There are two types of threads, foreground and background. Besides the main application thread, all threads created by calling a Thread class constructor are foreground threads. Background threads are the threads that are created and used from the ThreadPool, which is a pool of worker threads maintained by the runtime.

What is ThreadPool C#?

Thread pool in C# is a collection of threads. It is used to perform tasks in the background. When a thread completes a task, it is sent to the queue wherein all the waiting threads are present. This is done so that it can be reused.

How many tasks can a thread handle?

If by "in parallel" you mean "processed in parallel" and if you consider awaited Tasks, then there is no upper-bound limit on how many tasks are being awaited - but only one will actually be executed per a single CPU hardware-thread (usually 2x the CPU core count due to superscalar simultaneous multithreading, aka ...


2 Answers

I wouldn't even bother with ParameterizedThreadStart. Let the compiler do the dirty work:

private void ExecuteInBiggerStackThread(Action<Helper> action, Helper h)
{
    Thread bigStackThread = new Thread(() => action(h), 1024 * 1024);

    bigStackThread.Start();
    bigStackThread.Join();
}

Of course, you could carry this a step further and change the signature to:

private void ExecuteInBiggerStackThread(Action action) { ... }
like image 186
Toby Avatar answered Sep 20 '22 19:09

Toby


Something like this ought to do the trick:

private void ExecuteInBiggerStackThread(Action<Helper> action, Helper h)
{
    var operation = new ParameterizedThreadStart(obj => action((Helper)obj));
    Thread bigStackThread = new Thread(operation, 1024 * 1024);

    bigStackThread.Start(h);
    bigStackThread.Join();
}
like image 37
Mark Seemann Avatar answered Sep 19 '22 19:09

Mark Seemann