user10446544
user10446544

Reputation:

How do I pass a function for background worker?

I want to create a function so everytime I can pass the function to call when I need run the function in backgroundWorker. like this.

void RunInBackgroundWorker(Func<object, DoWorkEventArgs, bool> do_work)
{
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += do_work;
    worker.ProgressChanged += Worker_ProgressChanged;
    worker.WorkerReportsProgress = true;
    worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
    worker.RunWorkerAsync();
}

But visual studio complain that it cannot implicitly convert type 'System.Func' to 'System.ComponentModel.DoWorkEventHandler'

Is that possible to do it in this way? How to correctly pass the function in?

Upvotes: 1

Views: 225

Answers (2)

user10446544
user10446544

Reputation:

I actually solved it. Instead of passing a funtion (Func do_work), I pass (DoWorkEventHandler do_work) and it works!

Upvotes: 0

Blindy
Blindy

Reputation: 67380

worker.DoWork += (s, e) => do_work(s, e);

Also this has nothing to do with WPF.

Upvotes: 2

Related Questions