One of the threads does not allow the .NET program to stop completely. How to determine which one?

I have a great application with many features. Sometimes, when you close the main form, it comes out smoothly. In other cases, the form closes, but the program remains on. What debugging tools in Visual Studio 2010 can identify inappropriate threads?

+6
source share
5 answers

Your application will not exit until all threads with IsBackground == false have completed.

You can use the Threads window when the VS debugger is connected to see which threads are still running.

VS Menu: Debugging → Windows → Themes.

+1
source

I wrote the following code to keep track of which threads I created, and to warn me if any of them did not turn off when exiting.

It works flawlessly, and it has the advantage of calling threads in the Visual Studio debugger, which makes debugging easier.

To use it, call the following in each thread that you create manually:

 Thread x; // Yada yada create the thread. // Make sure you set ".IsBackground" property to "true". x.MyRememberThreadUntilShutdown("name which is visible in VS debugger"); 

Then call the following command when exiting:

 OnShutdownCheckThreadsForExit(); 

Here is a helper class:

  using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace MyShared.MyHelper { /// <summary> /// Helper classes for threads. /// </summary> public static class MyThread { /// <summary> /// Remembers a thread, until shutdown. /// On shutdown, we can double check that all of the threads we created have been shut down. /// </summary> /// <param name="thread">Thread we want to remember until shutdown.</param> /// <param name="newName">New name for thread.</param> public static void MyRememberThreadUntilShutdown(this Thread thread, string newName) { // Check whether the thread has previously been named // to avoid a possible InvalidOperationException. if (thread.Name == null) { thread.Name = "MyThread" + newName; } else { Console.Write("Error E20120118-1914. Unable to rename thread to \"{0}\" as it already has a name of \"{1}\".\n", newName, thread.Name); } ThreadList[newName] = thread; } /// <summary> /// This stores a list of all the threads we have running in the entire system. /// </summary> private static readonly Dictionary<string, Thread> ThreadList = new Dictionary<string, Thread>(); /// <summary> /// On program exit, check that all of the threads we started have been exited. /// </summary> public static bool OnShutdownCheckThreadsForExit() { if (ThreadList.Count != 0) { foreach (var thread in ThreadList) { if (thread.Value.IsAlive) { Console.Write("Error E20120119-8152. Thread name \"{0}\" was not shut down properly on exit.\n",thread.Key); } } return false; } else { return true; } } } } 
+1
source

If it sometimes closes smoothly, and not at another time .. I would see how you release or release objects and / or leave the application. Do you have any code, for example, where it will read

 Environment.Exit(0); for example..? 
0
source

I recently realized that you can also use the Debug Location toolbar to watch running threads. It is also very convenient when you want to see which specific method / operation the thread is currently executing.

For ease of identification, you can set the Name property of your Thread object. I mainly use the this prefix as the prefix, followed by the method called by the thread.

  var thread = new Thread(...) { Name = this + ".Connect" }; 
0
source

You can skip all open threads and close them.

Do this: Process.GetCurrentProcess (). That

Another alternative would be to terminate the process:

Process.GetCurrentProcess () kill () ;.

-2
source

Source: https://habr.com/ru/post/904485/


All Articles