简体   繁体   中英

How to send data from business logic class to UI while using BackgroundWorker in C#

I've two classes in my C# project. One for UI and another for Business Logic (BL). I've used BackgroundWorker in the UI project to call a method in BL which will handle a time consuming task (database backup). However, I couldn't send percentage completion from the BL to UI. What is the best way to achieve this?

Note: UI references the BL project

尝试Dispatcher.Invoke来写UI到Dispatcher.Invoke(()=> Status.Content =“ your text”);

In winforms you typically use a pattern similar to the following in your control/form.

public void UpdatePercentage(double percentage) {
    if (InvokeRequired) {
        Invoke(new Action<double>(UpdatePercentage), percentage);
        // You could also use BeginInvoke if you don't care about the return value
        return;
    }
    // we are now on the ui thread and can update what we want
    myLabel.Text = Math.Round((percentage * 100), 2).ToString();
}

References:

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