I am currently trying to make a regular function run as an anonymous BackgroundWorker's DoWork event. The issue I have is that the method is not running at all. The current code I have is as follows;-
public class Worker
{
BackgroundWorker worker;
public Worker(Func<bool> action)
{
worker = new BackgroundWorker();
worker.DoWork += (sender, e) => e.Result = action;
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
this.action = action;
}
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Console.WriteLine("Thread completed : "+ e.Result.ToString());
}
public void DoWork()
{
Console.WriteLine("worker thread: working...");
worker.RunWorkerAsync();
//Wait for worker to complete
do { } while (worker.IsBusy);
}
}
The function is passed like this:-
Worker workerObject = new Worker(new Func<bool>(() => methodThatReturnsBool(param1, param2)));
Thread workerThread = new Thread(workerObject.DoWork);
workerThread.Start();
How is it possible to pass the method and have it run within the background worker?