multithreading - C# Before closing application how to let background worker finish saving application state -


in c# desktop application, backgroundworker responsible saving application state being called in 2 situations. once while application running. works fine. other when application being closed, backgroundworker called save application state before starts saving, application closed , nothing gets saved.

i tried solve using autoreset event class in dowork , runworkercompleted didnt work because application closed before backgroundworker save thing.

question - how can make main thread wait until backgroundworker finishes saving?

private void frmmain_formclosing(object sender, formclosingeventargs e) {     this.saveahtoolstripmenuitem_click(this, e); }  private void saveahastoolstripmenuitem_click(object sender, eventargs e) {     this.backgroundworkermain1.runworkerasync(args); }  private void backgroundworkermain1_dowork(object sender, doworkeventargs e) {     savemethod(); }  private void backgroundworkermain1_runworkercompleted(object sender, runworkercompletedeventargs e) {     showresultofsaving(); } 

is winforms?

maybe register onclosing event. within it, set private property isclosing true. mark eventhandler e e.handled = true.

register backgroundworker event runworkercompleted. within it, check if isclosing property set , in case mainform.close() application.

edit:

using system; using system.threading; using system.windows.forms; using system.componentmodel;  namespace bgworker {     public partial class form1 : form     {         backgroundworker _bgworker;         bool _ineedtocloseafterbgworker;          public form1()         {             initializecomponent();         }          void form1_load(object sender, eventargs e)         {             _bgworker = new backgroundworker();             _bgworker.dowork += _bgworker_dowork;             _bgworker.runworkercompleted += _bgworker_runworkercompleted;         }          void _bgworker_runworkercompleted(object sender, runworkercompletedeventargs e)         {             messagebox.show("done!");              if (_ineedtocloseafterbgworker)                 close();         }          void _bgworker_dowork(object sender, doworkeventargs e)         {             // long lasting work             thread.sleep(2000);         }          void btnworkit_click(object sender, eventargs e)         {             // note how form remains accessible             _bgworker.runworkerasync();         }          void form1_formclosing(object sender, formclosingeventargs e)         {             if (_ineedtocloseafterbgworker || _bgworker.isbusy) return;             e.cancel = true;             _ineedtocloseafterbgworker = true;             _bgworker.runworkerasync();         }     } } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -