Prev: DeflateStream & MemoryStream?
Next: How can I place text in the rowheader column of my DataGridView in C#
From: Rob on 6 Mar 2006 12:52 Hello, I've got an issue where a process in a third party application has a dll which while exiting sort of freezes and runs away with processor cycles. i've written a block of code so that I can drill down into the process, and get the offending Thread's ID (since the only ones that will have the issue have higher User processor Time). But, I can't figure out how to have my .Net app 'kill' or 'suspend' a Thread based on the Id I have gotten. I see a lot of samples on how to do this with treads your application starts itself, but nothing 'external' to the app. Anyone able to lend a hand? Here's my base routine..... private static void DoSuspendThread(string processName) { // Suspend thread Console.WriteLine("Passed " + processName); try { Process[] myProcesses = Process.GetProcessesByName(processName); foreach (Process myProcess in myProcesses) { foreach (ProcessThread t in myProcess.Threads) { // info += myProcess.ProcessName + " " + t.Id + " " + t.UserProcessorTime + Environment.NewLine; TimeSpan time = new TimeSpan(0, 1, 0, 0, 0); if ((t.UserProcessorTime > time) && (t.ThreadState == System.Threading.ThreadState.Running)) { // Kill or suspend here } } } } catch (Exception e) { Console.WriteLine("Exception " + e.Message); } }
From: Markus Stoeger on 6 Mar 2006 14:47 Rob wrote: > Hello, > > I've got an issue where a process in a third party application has a dll > which while exiting sort of freezes and runs away with processor cycles. > > i've written a block of code so that I can drill down into the process, and > get the offending Thread's ID (since the only ones that will have the issue > have higher User processor Time). > > But, I can't figure out how to have my .Net app 'kill' or 'suspend' a Thread > based on the Id I have gotten. I see a lot of samples on how to do this with > treads your application starts itself, but nothing 'external' to the app. > > Anyone able to lend a hand? I'd definitely get them to fix their code instead of creating ugly workarounds. No piece of code lasts longer than a workaround. Max
From: "Yuan Ren[MSFT]" on 7 Mar 2006 05:58 Hi, Thanks for your post! From your description, my understanding is that you want to kill some threads from another .NET application. If I have misunderstood anything, please let me know. As far as I know, if you want to kill the thread, you have to use the Win32 API to approach this. I'll explain this limitation later. First of all, I don't recommend you kill the thread in the foreach statement. I suggest you save the thread id which you want to kill in some structures such as array or array list. Then, you can call OpenThread method to get the handle of the thread. After performing, please call TerminateThread function to kill the thread directly. You can obtain more information about these two methods by accessing link below: OpenThread: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/bas e/openthread.asp TerminateThread: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/bas e/terminatethread.asp BTW, you should use these methods by P/Invoke. If you have anything unclearly about the P/Invoke, please feel free to let me know. Actually, this way is not recommend because the ProcessThread is supposed only to get some information but not performing manipulations. That's way there is no abort, suspend, join method in the ProcessThread unlike the Thread class. So, the better way is get the fix or workaround from the software vendor as Max mentioned. Thanks for your understanding! Regards, Yuan Ren [MSFT] Microsoft Online Support ====================================================== PLEASE NOTE the newsgroup SECURE CODE and PASSWORD were updated on February 14, 2006. Please complete a re-registration process by entering the secure code mmpng06 when prompted. Once you have entered the secure code mmpng06, you will be able to update your profile and access the partner newsgroups. ====================================================== When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from this issue. ====================================================== This posting is provided "AS IS" with no warranties, and confers no rights. ======================================================
From: Rob on 7 Mar 2006 07:26 I'm not sure if it makes a difference, but its not a thread from another .NET application. I know ideally it would be best for the vendor to just 'fix' the problem, but, they are having a hard time admitting to the problem, and discovering the cause (and their first answer is 'upgrade' which could prove even more problematic than killing threads that are looking like they've done their task and are exiting). P/Invoke does look like the road to go... just a long dark and scary road. Thanks for the assist, Rob
From: Willy Denoyette [MVP] on 7 Mar 2006 07:45 Honestly, I don't think this is the road to go, take a look at the description of TerminateThread, especially the remarks, if these aren't enough reason NOT to kill arbitrary threads in a another process, well, go ahead, I wish you all the luck. Anyway, you need to find out what's really hapening, I'm not entirely clear on what you mean with "... has a dll which while exiting ...", what exactly do you mean with that? Another thing which I'm not sure about is who creates the thread, the DLL code or the client code? And is the client code also a third party? Willy. "Rob" <LandstarRob(a)nospam.nospam> wrote in message news:4FDA261A-7EB4-4DFB-A870-1BA9B07DF2E5(a)microsoft.com... | I'm not sure if it makes a difference, but its not a thread from another ..NET | application. | | I know ideally it would be best for the vendor to just 'fix' the problem, | but, they are having a hard time admitting to the problem, and discovering | the cause (and their first answer is 'upgrade' which could prove even more | problematic than killing threads that are looking like they've done their | task and are exiting). | | P/Invoke does look like the road to go... just a long dark and scary road. | | Thanks for the assist, | | Rob |
|
Next
|
Last
Pages: 1 2 Prev: DeflateStream & MemoryStream? Next: How can I place text in the rowheader column of my DataGridView in C# |