c# - ThreadState exception occurs when showing a form -


i have form take few seconds load. therefore, want show little form text 'loading, please wait'. when form finished loading, loading form must closed.

so, made simple class shows loading form in thread:

public class showloadingform {     thread _thread;      public void show()     {         try         {             _thread = new thread(new threadstart(showform));             _thread.setapartmentstate(apartmentstate.sta);             _thread.isbackground = true;             _thread.start();         }         catch (exception ex)         {             errormessages.unknownerror(true, ex);         }     }      private void showform()     {         loadingform f = new loadingform();         f.topmost = true;         f.showintaskbar = true;         f.settext(" loading... ");         f.show();     }      public void close()     {         _thread.abort();     } } 

in main form have:

_loadingform = new showloadingform(); _loadingform.show(); 

but. after piece of code, on main form: this.opacity = 0;. @ point, can see in debugger thread stopped working , threadstateexception thrown , loading form disappeared.

why this?

your program bombs because abort thread not take care of window. liable try run code because of windows notification, nosedive on threadstateexception because thread aborted. cannot end thread aborting it.

here's general class solve problem, takes care of shutting down waiting form , thread cleanly.

using system; using system.drawing; using system.windows.forms; using system.threading;  class loader : idisposable {     private autoresetevent initialized = new autoresetevent(false);     private form loadform;     private rectangle ownerrect;     private bool closeokay;      public loader(form owner, form pleasewait) {         if (pleasewait.isdisposed) throw new invalidoperationexception("create *new* form instance");         loadform = pleasewait;         loadform.topmost = true;         loadform.showintaskbar = false;         loadform.startposition = formstartposition.manual;         ownerrect = new rectangle(owner.location, owner.size);         loadform.load += delegate {             loadform.location = new point(                 ownerrect.left + (ownerrect.width - loadform.width) / 2,                 ownerrect.top + (ownerrect.height - loadform.height) / 2);             initialized.set();         };         loadform.formclosing += new formclosingeventhandler((s, ea) => {             ea.cancel = !closeokay;         });         var t = new thread(() => {             application.run(loadform);         });         t.setapartmentstate(apartmentstate.sta);         t.isbackground = true;         t.start();         initialized.waitone();     }      public void dispose() {         if (loadform == null) throw new invalidoperationexception();         loadform.invoke((methodinvoker)delegate {             closeokay = true;             loadform.close();          });         loadform = null;     }  } 

sample usage:

    private void button1_click(object sender, eventargs e) {         using (new loader(this, new loadingform())) {             system.threading.thread.sleep(3000);         }     } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -