c# - I can only close a form once, InvalidOperation Exception Invoke or BeginInvoke cannot be called on a control until the window handle has been created -


hi i'm opening form main form when user makes selection of menu item.

private void commtoolstripmenuitem_click(object sender, eventargs e)         {             command_form command_form1 = new command_form();             command_form1.showdialog();            // command_form1.dispose();    didn't         } 

inside form "command_form1" close when user clicks on close button

private void close_button_click(object sender, eventargs e)         {           this.close();    //i exception here          } 

this process works fine once on second closing of form (which hope different/new instance of form) error in title of post. output in debug window.

a first chance exception of type 'system.invalidoperationexception' occurred in system.windows.forms.dll

all topics list error go on not trying form has not been displayed happens when click on button in form. seem me pretty ensures form has been displayed if i'm able click button.

the other posts i've found list type of error go on making thread safe calls tried experiment didn't make difference.

private void close_button_click(object sender, eventargs e)             {     if (this.invokerequired)                 {                     closecallback d = new closecallback(close_button_click);                     invoke(d, new object[] { sender, e });                 }                 else                 {                     this.close(); 

i have multiple threads in application created controls i'm using not me explicitly. passing data serial port to/from form marshling received/sent data via delegate[s]. makes sense serial port run on different thread form why button click on form in diffrent thread form????

the whole thread thing confuzing how figure out threads originated , going on in threads didn't create explicitly? why need invoke form's close method via delegate? heck there can in multi threading environment thread safe how in know if i'm doing unsafe/safe if don't know what/where/why/who/when creating threads?

my guess close() call not throwing exception, happens after close(). have stepped code debugger see when fired?

as when need invoke...there 1 thread allowed make changes , access dynamic properties on gui, call gui thread. gui thread responsible updating layout, firing events buttons, etc. if ever access gui thread (like timer event) need use invoke() queue function run on gui thread. begininvoke queue function asynchronous (will queue function run on gui thread, not wait finish).

close_button_click called gui thread when button click event fires(unless explicitly call somewhere else in code behind, not recommended!), invokerequired=false in code above, , invoke code never executed.


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..." -