c# - "Invoke or BeginInvoke cannot be called on a control until the window handle has been created" only occurs the second time form opens -


the application developing plugin existing application , first time running plugin works great. when open plugin second time error:

invalidoperationexception unhandled - invoke or begininvoke cannot called on control until window handle has been created.

i understand race conditions , i've read, error occurs when trying access form element before handlecreated true, cannot figure out why happening second time open plugin.

here plugin code. error occurs when calling setprogressbar():

    private mainform mainform;      public void startplugin()     {         mainform  = new mainform (this);         mainform .showdialog();     }      public bool getjoinenabled()     {         mainform.setprogressbar(3);     } 

here's main form:

    private thread m_jointhread;     private joinplugin m_join;      public mainform(joinplugin zig)     {         m_join = zig;         initializecomponent();         m_jointhread= new thread(new threadstart(getjoindata));         m_jointhread.start();     }      private void getjoindata()     {        //get enable join data         bool result = m_join.getjoinenabled();     }      public void setprogressbar(int value)     {         setprogresscallback del = new setprogresscallback(setprogressbarcontrol);         this.invoke(del, value);     }      private void setprogressbarcontrol(int value)     {         progressbar.value = value;     } 

i'm guessing little, ran same issue time ago.

you starting thread in forms constructor:

m_jointhread.start(); 

this starts thread immediatedly , call invoke somewhere. @ time, form not initilized completely.
move code load event:

public zigbeejoinform_load() {     m_jointhread= new thread(new threadstart(getjoindata));     m_jointhread.start(); } 

this ensures form initialized , calling invoke safe then.


Comments