I use the code below to run a thread with multiple parameters:
public Thread StartTheThread(System.Windows.Threading.Dispatcher dispatcher, int param1, string param2)
{
Thread t = new Thread(() => Work(Maingrid.Dispatcher, param1, param2));
t.Start();
return t;
}
public delegate void delegate1(Color randomparam, Color secondparam);
public void Work(System.Windows.Threading.Dispatcher dispatcher, int param1, string param2)
{
dispatcher.Invoke(new delegate1(update), {Color.FromRgb(255, 255, 0),Color.FromRgb(170, 255, 170)});
}
public void update(Color randomparam, Color secondparam)
{
...
}
Creating a new thread normally requires either "ThreadStart" or "ParameterizedThreadStart" method. Threadstart method is for threads with no parameter, and parameterizedthreadstart method is for threads with only 1 parameter (as object). But I have different types of parameters. Since these methods are delegates, I tried to store the thread using a custom delegate to call later on:
public delegate void starterdelegate(System.Windows.Threading.Dispatcher dispatcher, int param1, string param2);
public Thread StartTheThread(int param1, string param2)
{
Thread t = new Thread(new starterdelegate(RealStart));
...
return t;
}
But in this case, compiler returns this error:
"Overload resolution failed because no accessible 'New' can be called with these arguments: 'Public Sub New(start As System.Threading.ParameterizedThreadStart)': Value of type 'ThreadTraining.MainWindow.starterdelegate' cannot be converted to 'System.Threading.ParameterizedThreadStart'. 'Public Sub New(start As System.Threading.ThreadStart)': Value of type 'ThreadTraining.MainWindow.starterdelegate' cannot be converted to 'System.Threading.ThreadStart'."
What I mean is that there's no problem with running thread with multiple parameters, but when I want to store the thread t, I don't want to submit parameters because they will be changed until next time I run the thread. If I use ParameterizedThreadStart method and don't submit parameters, compiler will throw a signature error. If I don't use one of the methods required, compiler will throw an overload resolution fail error.
I don't even know why this:
Thread t = new Thread(() => Work(Maingrid.Dispatcher, param1, param2));
works in first place. How is parameter of the "new Thread" here compatible with the methods required? I found this line of code on this page: https://stackoverflow.com/a/1195915/2770195
Any advice?