简体   繁体   中英

Loading Dialog in BackgroundWorker

For numerous reasons i cannot run the main portion of the code in a background worker, i can instead run a Loading dialog in the background worker but need to close the dialog after the execution of the main code portion. I've implimented the following but I'm not sure how to forcefully close the background worker after the code execution:

    LoaderWorker = new BackgroundWorker();

    //Loading is the Form
    Loading Loader = new Loading("Daten Exportieren");

    LoaderWorker.DoWork += (s, args) =>
    {
    //Show Loading Dialog
    Loader.Show();
    };

    ExecuteMainTasks();

    //Here i need to stop the backgroundworker after the method above is complete.

Is there a solution to this with background worker or should i use a different approach to ensure i do not lock the UI thread for the loader. Remember...i cannot execute the main code in the backgroundworker so have to manage this somewhat backwards.

Thanks.

Control.Invoke
Control.BeginInvoke

(Windows Forms)

or

Disopatcher.Invoke
Dispatcher.BeginInvoke

(WPF)

or

CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync

(WinRT 😂)

Calling this from the background worker will execute the passed lambda / delegate in the UI thread. Simply perform progress / finished notifications this way from your background worker.

So, show your dialog in the UI thread, then start the background worker, from which you can use this way of executing code in the UI thread.

Just make sure that you synchronously stop the background worker when the user cacels / closes the dialog.

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