0

I have an app that is not closing properly. There is a user control that appears twice in the form. Using debug I can see the following code in Form1.Designer executed twice:

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

This makes sense to me as it is disposing of the two usercontrols. After this happens the code execution returns to the line:

Application.Run(new Form1());

In this code block.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

I get no error messages.

I do find others recommending someone Application.Current.Shutdown() in a form closing event but that seems like it does not solve root cause.

How can I debug this issue further? Not sure where to start.

Eric Snyder
  • 1,816
  • 3
  • 22
  • 46
  • 1
    You might have background threads still executing... check the thread window in visual studio – Daniel Mar 11 '19 at 20:22
  • 1
    To confirm, when you press the Close button (in the top-right corner) the window does close, but your process stays running? – Dai Mar 11 '19 at 20:31
  • 1
    Correct. Windows closes but the app stays running. I am checking threads now as @Daniel suggested and I do see a thread that is running in a third party DLL. I don't seem to be able to close it down though. – Eric Snyder Mar 11 '19 at 20:33
  • 1
    @EricSnyder Does your code create that thread or is it the third-party library? If the library is creating its own thread, does the library provide any way to stop the thread (e.g. is there an `IDisposable` that you're not disposing-of? Does the third-party library use any async code?) – Dai Mar 12 '19 at 07:21
  • @Dai - Sorry for the tardy response. I have a background thread running. It is from a third party library. The library discovers devices attached to the network, USB, USB3 and other interfaces. I have a class that is doing the discovery work. As far as I can tell I am shutting down everything properly in the class using the third party methods for stopping. After I stop everything I even dispose the object and then assign it null value. I just checked after doing all that and there is still a thread running from that object. – Eric Snyder Mar 22 '19 at 22:20

0 Answers0