c# - WPF multi-thread progress update -
i'm need display form of feedback user, while small process (7-10 seconds) takes place in background.
i had no issues in past using separate threads , backgroundworkers in windows forms, proving difficult in wpf.
i have read many articles, in respect, , how should using dispatchers in wpf start new thread, etc. however, when try use backgroundworker display form of waiting image feedback, remains static.
i don't believe matters, uses mui firstfloor (https://github.com/firstfloorsoftware/mui).
i'm trying use built-in progressring feature (which works no problems when run within same thread , there no other major tasks running in background.
adding backgroundworker, brings exception due cross thread access of objects, though many blogs states backgroundworks in wpf cross thread aware , safe run.
the following closest code generates need.
private async void mytaskprocess() { await dispatcher.begininvoke(dispatcherpriority.send, new threadstart(() => { try { //update waiting ring image progressring.isactive = true; } catch { progressring.isactive = false; messagebox.show("exception thrown"); } })); await dispatcher.begininvoke(dispatcherpriority.background, new threadstart(() => { try { //run main ms excel export function export2excel(); progressring.isactive = false; } catch { messagebox.show("exception thrown"); } })); }
any feedback appreciated.
the way in modern wpf application start new task
in work; under covers perform work on thread pool thread:
task.factory.startnew(this.dowork)
now in dowork
to report progress invokeasync
main thread whenever porgress count changes:
void dowork() { foreach(var item in this.workitems) { // // report progress ++progress application.current.dispatcher.invokeasync(() => this.progress = progress); } }
Comments
Post a Comment