Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

c# - Killing all threads that opened by application

I have some really big application mixture of c# and 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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...