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

有帮助吗?

解决方案

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; }
}

其他提示

You probably want to use new ParameterizedThreadStart(hueckel_operator) to be safe and then have the workerThread.Start(); pass the parameters in an array or list.

I think that the way you are calling your new thread is not expecting to receive any parameters in the function and therefore it cannot find your function. If you want to pass parameters to your new thread you should use an array of objects and pass that to the function.

That's because a ThreadStart delegate is supposed to take no arguments but your function hueckel_operator takes two.

To pass arguments to your thread function, simply pass it to the thread constructor using a lambda expression like so:

public static void parametrized(int one, int two)
{
    //perform computation
}

Then pass it to the Thread constructor wrapped in a lambda expression:

int arg1 = 4;
int arg2 = 2;
Thread t = new Thread(new ThreadStart(() => parametrized(arg1, arg2)));

The expression () => parametrized(arg1, arg2) creates an anonymous function that takes no arguments (just like our Thread is expecting), and then simply calls the function we actually want to call from its body with the provided arguments.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top