I want to get all the thread of the current process, and view the status, or a specified thread running time
Process the current=Process. GetCurrentProcess ();
ProcessThreadCollection allThreads=current. The Threads;
Now I can get all the thread thread ID, but can't identify whether a thread is my target thread,
Because the thread is started to specify the name of the thread, so that it can be judged by specifying the name of a thread state, so I want to get a thread by thread ID name, so that you can identify my goals thread,
But how to get the thread by thread ID name?
CodePudding user response:
Directly add a thread to the List
In Thread tempThread=threadList. Where (x=& gt; X.N ame=="test thread name"). FirstOrDefault (); If (tempThread!=null) { Console. WriteLine (tempThread. ThreadState); } CodePudding user response:
Are you sure you given name to each thread, thread the default name is likely to be empty, if you want to rely on for thread gives names to locate the thread, the thread ID and deposited into List , is not the same effect? CodePudding user response:
reference 1st floor chb345536638 response: add threads to List< directly; Thread> In Thread tempThread=threadList. Where (x=& gt; X.N ame=="test thread name"). FirstOrDefault (); If (tempThread!=null) { Console. WriteLine (tempThread. ThreadState); } What has happened is that there is a monitoring thread, to monitor the state of the other threads (part of the thread), the operation of the program process, at any time there will be new thread start and the end, If in this way to maintenance is too complex, so want to along with it, CodePudding user response:
O great god answer ~ ~ ~ ~ ~ ~ CodePudding user response:
Define a global map, among them with thread for the key, I the name of the thread for value, each time a new thread start he saved in, thread end is deleted, so you can confirm whether your target thread by name, CodePudding user response:
reference 5 floor lye2000000_super reply: define a global map, among them with thread for the key, I the name of the thread for value, each time a new thread start he saved in, thread end is deleted, so you can confirm whether your target thread by name, This method is similar to the first floor provides methods, is will start the thread, or think it is too complex, want to find a more quick and convenient solution, if there is no better solution, can only do so CodePudding user response:
refer to 6th floor weixin_41227170 response: Quote: refer to the fifth floor lye2000000_super reply: Define a global map, among them with thread for the key, I the name of the thread for value, each time a new thread start he saved in, thread end is deleted, so you can confirm whether your target thread by name, This method is similar to the first floor provides methods, is will start the thread, or think it is too complex, want to find a more quick and convenient solution, if there is no better solution, can only do so How is it possible that there will be a convenient method? Don't keep you without the basis of comparison,,,,,,, can not be conducted behind,,, CodePudding user response:
I don't know what you said is so complicated, you create a new thread to add you trouble? And got the ProcessThread ProcessThreadCollection inside System. Diagnostics. The ProcessThread And System. The Threading. Thread Two is not a type, there is no any contact System. Diagnostics. The ProcessThread said current unmanaged threads System. When the Thread is managed threads ProcessThread also don't have any name of the attribute CodePudding user response:
Actually very good solve this problem, also need not obtain the name of the thread, when you set the name of the thread, the thread name set like the thread ID is ok! This ID can be directly as a name CodePudding user response:
Perhaps the original poster is like I want to check the operating condition of the thread? Is the thread in the process we use too many, will appear more often lead to no good control of the thread cannot be recycled, over time, piling up, so I want to find out why Wanted to by setting the thread Name for a note for printing test, imagined ProcessThreadCollection could not see the Name Upstairs method through the List. The Add (Thread) can be achieved, but some force ~ Normally can get the current Thread's Name, should be there will be a way to get all the Thread's Name