其中一个线程阻止.net程序完全停止.如何识别哪一个

本文关键字:何识别 哪一个 识别 程序 一个 线程 net | 更新日期: 2023-09-27 18:18:45

我有一个包含许多函数的大型应用程序。有时,当您关闭主表单时,它会顺利退出。其他时候,表单关闭,但程序仍在运行。Visual Studio 2010提供了哪些调试工具来识别行为不端的线程?

其中一个线程阻止.net程序完全停止.如何识别哪一个

您的应用程序将不会退出,直到所有具有IsBackground == false的线程都完成。

你可以在附加VS调试器时使用Threads窗口来查看哪些线程仍在运行。

VS菜单:Debug -> Windows -> Threads.

我编写了以下代码来跟踪我创建的线程,并在退出时警告我它们中的任何一个没有关闭。

它完美地工作,它的优点是可以在Visual Studio调试器中命名线程,这使得调试更容易。

要使用

,在您手动创建的每个线程上调用以下命令:

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

然后,在退出时调用以下命令:

OnShutdownCheckThreadsForExit();

下面是helper类:

    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;
                }
            }
        }
    }

如果它有时顺利关闭,而不是在其他时候…我会看看你是如何释放或释放对象和/或退出应用程序的。你有没有一些代码,例如,它会读取

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

最近我意识到,您还可以使用调试位置工具栏来观察正在运行的线程。当您想要查看线程当前正在执行的特定方法/操作时,这也非常方便。

为了方便识别,您可能需要设置Thread对象的Name属性。大多数情况下,我使用this作为前缀,后跟线程调用的方法。

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

你可以遍历所有打开的线程并关闭它们。

这样做:Process.GetCurrentProcess () .Threads

另一种选择是终止进程:

Process.GetCurrentProcess () .Kill ();