c# - invoke causes Cannot access a disposed object -


i have form in load page create thread , delegate show position every time updated :

   private delegate void updatelistboxdelegate();   private updatelistboxdelegate updatelistbox = null;  private void frmmain_load(object sender, eventargs e)         {              pictureboxonlinetrain.parent = pictureboxmetromap;             updatelistbox = new updatelistboxdelegate(this.updatestatus);             // initialise , start worker thread             workerthread = new thread(new threadstart(this.getonlinetrain));             workerthread.start();          } 

so in load form called thread:

   private bool stop = false;     public void getonlinetrain()     {         try         {             while (stop!=true)             {                 timetablerepository objtimetablerepository = new timetablerepository();                 onlinetrainlist = objtimetablerepository.getall().tolist();                 objtimetablerepository = null;                 if(stop!=true)                 invoke(updatelistbox);                 else this.dispose();               }         }         catch(exception a)         {         }      } 

my problem when want show form got error:

        stop = true;         frmpath frmpath = new frmpath();         frmpath.showdialog(); 

this error

  cannot access disposed object  -{system.objectdisposedexception: cannot access disposed object. object name: 'frmmain'.    @ system.windows.forms.control.marshaledinvoke(control caller, delegate method, object[] args, boolean synchronous)    @ system.windows.forms.control.invoke(delegate method, object[] args)    @ system.windows.forms.control.invoke(delegate method)    @ presentationlayer.prelayer.frmmain.getonlinetrain() in d:\tfs project\railway\shirazmetro\presentationlayer\prelayer\frmmain.cs:line 167} 

i got error in getonlinetrain method .

i frmpath in formload when put line got above error ,but when clear line every thing works fine!!!!!!!

private void frmline_load(object sender, eventargs e) {     txtnumber.focus();      gvlistline.datasource = objlinerepository.getall().tolist();  } 

best regards

your thread loops endless (because of while (true)). once close form (i assume when "want show form" close old form) , call dispose on (probably done framework). thread continue working form since not stop. next time calls invoke crash.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -