杀死应用程序打开的所有线程

时间:2023-01-21 11:40:46

I have some really big application mixture of c# and j#.

我有一些非常大的应用程序混合使用c#和j#。

Sometimes when I close it, there are some threads that are not closed and they are hanging in the task manager and it is impossible to kill them from there.

有时当我关闭它时,有一些线程没有关闭,它们挂在任务管理器中,不可能从那里杀死它们。

I have really problem to find all those threads and add them to the closing event .

我真的有问题找到所有这些线程并将它们添加到结束事件中。

Is there some way violently kill all threads that were opened by application in the closing event ?...

是否有某种方式猛烈杀死在关闭事件中由应用程序打开的所有线程?...

Thanks.

谢谢。

Is there maybe some Tool that can tell me what threads are opened while i close the application and who openned them ?

是否有一些工具可以告诉我在关闭应用程序时打开了哪些线程?

6 个解决方案

#1


45  

This shouldn't be happening, and if it is, you're trying to address it the wrong way.

这不应该发生,如果是,你试图以错误的方式解决它。

When your application exits, the .NET Framework will automatically kill any threads whose IsBackground property is set to "True". Designate each of your worker threads as background threads, and you won't have this problem anymore. Taking advantage of the BackgroundWorker class and the ThreadPool class, which automatically create background threads, is a much better option.

当您的应用程序退出时,.NET Framework将自动终止其IsBackground属性设置为“True”的所有线程。将每个工作线程指定为后台线程,您将不再遇到此问题。利用BackgroundWorker类和自动创建后台线程的ThreadPool类是一个更好的选择。

Otherwise, you need to clean up your foreground threads explicitly. A properly designed application will do its own bookkeeping and have a deterministic, structured way of ensuring that all its threads have been closed before exiting the Main method. This is what you should be doing anyway if your threads require a graceful termination.

否则,您需要明确清理前台线程。设计合理的应用程序将执行自己的簿记,并具有确定的结构化方法,以确保在退出Main方法之前已关闭其所有线程。如果您的线程需要正常终止,那么这就是您应该做的事情。

Killing the process is a very bad idea, as is letting your threads run about willy-nilly in your application.

杀死这个过程是一个非常糟糕的主意,因为让你的线程在你的应用程序中无所畏惧地运行。

#2


13  

You can use : Environment.Exit(0); , that will shutdown the application if threads are running and wont cause any problems.

您可以使用:Environment.Exit(0); ,如果线程正在运行并且不会导致任何问题,那将关闭应用程序。

#3


4  

Well, you could call Application.Exit() but that won't be of much help. The bottom line is that you have to gracefully close all of the threads yourself if you want to do things properly.

好吧,你可以调用Application.Exit(),但这不会有太大帮助。最重要的是,如果你想正确地做事,你必须自己优雅地关闭所有的线程。

#4


2  

You should close your threads gracefully, but just want you and others to know the way that is not recommended but possible:

你应该优雅地关闭你的线程,但只是希望你和其他人知道不推荐但可能的方式:

on your OnClose Handler:

在您的OnClose处理程序上:

System.Diagnostics.Process.GetCurrentProcess().Kill();

I totally prefer Cody Gray way of doing it.

我完全喜欢Cody Gray的做法。

#5


0  

My 2 cents... to all answers...

我的2美分...所有答案......

Try to force SHUTDOWN

尽量强制关机

Put into void CurrentApplication_Exit(object sender, ExitEventArgs e) and private void Window_Closing(object sender, CancelEventArgs e) those lines

放入void CurrentApplication_Exit(对象发送者,ExitEventArgs e)和私有void Window_Closing(对象发送者,CancelEventArgs e)这些行

System.Windows.Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
System.Windows.Application.Current.Shutdown();

#6


0  

internal void Close()
{
    Dispatcher.CurrentDispatcher.Thread.Abort();
    Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
    Application.Current.Shutdown();
}

#1


45  

This shouldn't be happening, and if it is, you're trying to address it the wrong way.

这不应该发生,如果是,你试图以错误的方式解决它。

When your application exits, the .NET Framework will automatically kill any threads whose IsBackground property is set to "True". Designate each of your worker threads as background threads, and you won't have this problem anymore. Taking advantage of the BackgroundWorker class and the ThreadPool class, which automatically create background threads, is a much better option.

当您的应用程序退出时,.NET Framework将自动终止其IsBackground属性设置为“True”的所有线程。将每个工作线程指定为后台线程,您将不再遇到此问题。利用BackgroundWorker类和自动创建后台线程的ThreadPool类是一个更好的选择。

Otherwise, you need to clean up your foreground threads explicitly. A properly designed application will do its own bookkeeping and have a deterministic, structured way of ensuring that all its threads have been closed before exiting the Main method. This is what you should be doing anyway if your threads require a graceful termination.

否则,您需要明确清理前台线程。设计合理的应用程序将执行自己的簿记,并具有确定的结构化方法,以确保在退出Main方法之前已关闭其所有线程。如果您的线程需要正常终止,那么这就是您应该做的事情。

Killing the process is a very bad idea, as is letting your threads run about willy-nilly in your application.

杀死这个过程是一个非常糟糕的主意,因为让你的线程在你的应用程序中无所畏惧地运行。

#2


13  

You can use : Environment.Exit(0); , that will shutdown the application if threads are running and wont cause any problems.

您可以使用:Environment.Exit(0); ,如果线程正在运行并且不会导致任何问题,那将关闭应用程序。

#3


4  

Well, you could call Application.Exit() but that won't be of much help. The bottom line is that you have to gracefully close all of the threads yourself if you want to do things properly.

好吧,你可以调用Application.Exit(),但这不会有太大帮助。最重要的是,如果你想正确地做事,你必须自己优雅地关闭所有的线程。

#4


2  

You should close your threads gracefully, but just want you and others to know the way that is not recommended but possible:

你应该优雅地关闭你的线程,但只是希望你和其他人知道不推荐但可能的方式:

on your OnClose Handler:

在您的OnClose处理程序上:

System.Diagnostics.Process.GetCurrentProcess().Kill();

I totally prefer Cody Gray way of doing it.

我完全喜欢Cody Gray的做法。

#5


0  

My 2 cents... to all answers...

我的2美分...所有答案......

Try to force SHUTDOWN

尽量强制关机

Put into void CurrentApplication_Exit(object sender, ExitEventArgs e) and private void Window_Closing(object sender, CancelEventArgs e) those lines

放入void CurrentApplication_Exit(对象发送者,ExitEventArgs e)和私有void Window_Closing(对象发送者,CancelEventArgs e)这些行

System.Windows.Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
System.Windows.Application.Current.Shutdown();

#6


0  

internal void Close()
{
    Dispatcher.CurrentDispatcher.Thread.Abort();
    Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
    Application.Current.Shutdown();
}