Search code examples
c#multithreadingstack-overflowthread-state

ThreadStateException occures when trying to start a process


I am trying to run my recursive function "hueckel_operator()" in another thread whose stack size is increased to 10.000.000. Firstly, hueckel_operator is called when I click on detect edges button. So I have created new thread in detect_edges_click() function as

 private void detect_edges_Click(object sender, EventArgs e)
        {
             var stackSize = 20000000;
             Thread workerThread = new Thread(new ThreadStart(hueckel_operator), stackSize);                          
                workerThread.Start();

        }

public void hueckel_operator(int counter4, int counter5)
{

}

But I get an error as "Error 22 No overload for 'hueckel_operator' matches delegate 'System.Threading.ThreadStart'"

In which way I can create a new thread to execute my recursive function on?

Instead of creating a new thread should I better increase the stack size of my main thread?

Or am I talking completely nonsense and I should go on reading more about threads?

Thanks in advance


Solution

  • Reading the MSDN of ThreadStart, we can see that the delegate's signature is:

    public delegate void ThreadStart()
    

    which your method does not respect, since it takes two parameters.

    If you want to pass parameters you can use ParameterizedThreadStart, but you still need to change your method signature to accept a single object parameter:

    public void hueckel_operator(object param)
    {
    }
    

    You could then encapsulate your two int parameters in a custom type:

    class ThreadParameter
    {
         public int I { get; set; }
         public int J { get; set; }
    }