0

Within a C# Form, I have a function that calls this.Close(), after which the parent reopens the Form. Things work great when this function is called from the click event of a button on the GUI and the Form reopens. About 90% of the time, when this function is called from a different thread such as the clock timer, reopening the Form results in a flash of the frame outline and a clean exit (no errors or exceptions). The call to this.Close() is the last thing in each function.

BTW, the parent creates the Form with: Application.Run(new frmServer());

I tried replacing Close with Dispose, calling Dispose both in the frame and after it returns, putting the call to close directly in the clock tick function instead of calling the common function. There is no difference.

  • Also if you are using a Timer in a winform you should likely be using this to ensure thread safety https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.timer – Igor May 04 '23 at 15:56
  • You should only be closing the form from the main UI thread. Use `BeginInvoke()` to call a method to close the form if `Control.InvokeRequired` is true. – Matthew Watson May 04 '23 at 16:04
  • To expand on what @MatthewWatson has said, if you want to access the form in any way, you need to do from the thread that created the form. – Flydog57 May 04 '23 at 16:10

0 Answers0