0

i have developed application which uses threads to finish the task and i want to close the running application in middle of the process its closes but thread is running in background.

now i want to show active threads by which my application is created. but difficulty is, when i start the application from vs threads are shown in task manager like 14-19 threads without running the application.

can u tell me to display only those threads which is created by my application and not by system.

Mukesh
  • 45
  • 3
  • 10
  • possible duplicate of [how to find the active thread count?](http://stackoverflow.com/questions/2744295/how-to-find-the-active-thread-count) – Cᴏʀʏ Sep 18 '12 at 05:45
  • You want threads or threads count? Asking because just count is easy to do. But if you want to achieve something like closing all threads on closing application, there are better solutions along with telling you the thread count. What you wanna achieve? – Sami Sep 18 '12 at 06:04
  • Your sentence "now i want to show active threads by which my application is created. but difficulty is, when i start the application from vs threads are shown in task manager like 14-19 threads without running the application" seems not much consistent to me. – Sami Sep 18 '12 at 06:06
  • i want to show thread count. for ex if i click close button then i want to show information that there is this much of threads running after that it will close. – Mukesh Sep 18 '12 at 06:18

3 Answers3

4

Why not use Process.Threads? Following should give you all threads that are running under your process.

var myProcess = Process.GetCurrentProcess();
var myThreads = myProcess.Threads;
loopedcode
  • 4,863
  • 1
  • 21
  • 21
0

This one line maybe help you.

System.Diagnostics.Process.GetCurrentProcess().Threads.Count
Simon Edström
  • 6,461
  • 7
  • 32
  • 52
  • i tried this but i am not able to get actual thread which is created by my application – Mukesh Sep 18 '12 at 06:22
  • But what do you mean by created your application? This shows the current threads started by the current process. Including the *parent* thread that starts the others. – Simon Edström Sep 18 '12 at 10:47
0

I would approach this problem slightly differently. I would keep track of the amount of threads that I have started myself.

I hope I am right in assuming that you should have 1 UI thread, and then x new threads as you start them. If you want a total count of the threads you have started, perhaps increment a static int somewhere in your application with each thread that starts, and decrement that int with each thread that stops.

You will probably need to lock around the increment and decrement code for the static int to ensure that two threads do not change the value at the same time.

.Net has quite a few libraries which do this. If you have the time I suggest you read this article as it might give you a push in the right direction.

Dave Lucre
  • 1,105
  • 1
  • 14
  • 16