4

Lately I have been having problems with my application not shutting down properly. After it has been told to exit, when I look in the Task Manager the process is still running, and I am unable to kill the process.

Suddenly I realized a strange pattern. The shutdown problem only appeared if I had opened a OpenFileDialog anytime when the application was running. I debugged a bit and saw that some threads did not shut down after the application should have exited. Also, to my surprise, when I invoked OpenFileDialog.ShowDialog(), it spawned a lot of threads (See the pictures below). The threads are alive throughtout the lifetime of the application.

  • Why does OpenFileDialog spawn so many threads? And why are they not closed after the file dialog is closed.

  • How does the OpenFileDialog problem relate to my shutdown problem...?

Threads just before openFileDialog.ShowDialog(): Threads just before openFileDialog.ShowDialog()

Threads while the dialog is open: Threads while the dialog is open

Threads right after openFileDialog.ShowDialog() has returned: Threads right after openFileDialog.ShowDialog() has returned

Threads hanging after the application has been shut down: Threads hanging after the application has been shut down

Code for opening the dialog:

    private void startAllSequenceToolStripMenuItem_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofn = new OpenFileDialog();

        DialogResult result = ofn.ShowDialog();
        if (result == DialogResult.Cancel)
            return;

        MessageBox.Show("do stuff");
    }
shadow_map
  • 313
  • 3
  • 15
  • 2
    Maybe you could share that part of code where you invoke OpenFileDialog? – Rameez Ahmed Sayad Sep 24 '13 at 07:31
  • 4
    Disable all your shell extensions. Try your program on a machine that has no shell extensions. – David Heffernan Sep 24 '13 at 08:11
  • I've added the code where I invoke OpenFileDialog to the post. – shadow_map Sep 25 '13 at 07:39
  • Show the portion of code where your loop calls the OpenFileDialog or function that calls it. – Prix Sep 25 '13 at 07:48
  • It seems the problem only appears on one of my computers. I've tried disabling all shell extensions, but the problem is still there.. Hmm.. – shadow_map Sep 25 '13 at 13:22
  • It seems to be there may be a loop running while you invoke your Open Dialog. Check Your code or publish your code to inspect us. – tarzanbappa Jan 21 '14 at 06:24
  • 1
    I experienced the same problem and searched for an answer for a long time. Shell extensions could be the problem: I tested my app on an XP virtual machine without anything installed - there are 3 threads generated and the app terminates under a second. On my development machine with several extensions installed (e.g. tortoise svn, hardlink shellext, 7-zip, etc..) there are 20 more threads and it takes 15s to terminate. Sad thing is i don't see any solution for this... – JCH2k Jan 21 '14 at 12:42

2 Answers2

3

I search the web for this and found nothing but I fixed by issue by calling Dispose. Code below:

private void startAllSequenceToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenFileDialog ofn = new OpenFileDialog();

    DialogResult result = ofn.ShowDialog();
    if (result == DialogResult.Ok)
    {
        MessageBox.Show("do stuff");
    }

    // This one line seems to allow my application to exit cleanly in debug and release.
    // But I don't instantiate a new object.
    // I used the control on the form and called Dispose from form_closing.
    ofn.Dispose();
}
Jay Croghan
  • 445
  • 3
  • 16
  • Helpful answer, solved the problem for me. Would be nicer to wrap the dialog in a using block rather than calling Dispose directly. – rho21 Mar 13 '17 at 16:06
  • Yeah that's the more correct answer @rho21 but this worked quick for me at the time. – Jay Croghan Jul 12 '17 at 13:02
1

This thread OpenFileDialog/c# slow on any file. better solution? has some half decent answers. All in all, as a last resort check with ProcExp from sysinternals. Also, is it only slow in the debugger? If so I wouldn't worry about it since it doesn't affect your users. Just make sure to isolate from your other code so your team is not constantly tripping over it in the debugger since it is slow.

Community
  • 1
  • 1
therealjumbo
  • 1,079
  • 1
  • 10
  • 14