简体   繁体   中英

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?

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

同样,这与WPF无关。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM