Search code examples
c#.netwinformsformclosing

C# form doesn't close on FormClosing event


After I added the following code to my code-behind, my form doesn't get closed.

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
    MyThreadingObj.Dispose();
}

Solution

  • It sounds like adding the above code prevents your Form from closing. If so then the most likely cause is that the MyTHreadingObj.Dispose() statement is throwing an exception. Try wrapping the statement in a try/catch and seeing if this is the case

    try {
      MyThreadingObj.Dispose();
    } catch ( Exception e ) { 
      Console.WriteLine(e);
    }