473,324 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

C# Threading, and suspending or killing a thread

Rob
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);
}
}
Mar 6 '06 #1
8 12330
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
Mar 6 '06 #2
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/de...us/dllproc/bas
e/openthread.asp

TerminateThread:
http://msdn.microsoft.com/library/de...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.
================================================== ====

Mar 7 '06 #3
Rob
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

Mar 7 '06 #4
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" <La*********@nospam.nospam> wrote in message
news:4F**********************************@microsof t.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
|
Mar 7 '06 #5
Rob
The thread itself is created by a service process (a web server's ISAPI that
handles authentication). It makes some calls to a dll (msvcrt.dll) and the
info from ProcessExplorer shows it as msvcrt.dll!endthreadex+0x2f.

Other threads that are spawned like this go right back to zero CPU, and just
go away. But sometimes they get stuck (with 25-50% of the CPU) and just hang
around. I've use the Process Explorer tool to suspend and to kill the
threads, and its had no detrimental effect on the service. The web service
call that generated the issue completes successfully, and subsequent calls
all work fine. That's what I meant by 'while it was exiting'.

The reason we're looking at this is that these hanging threads can eat up
100% CPU with just 2 or 4 of these calls hung. Not a perfect reason to
terminate the thread, but, while we're waiting on the vendor, it beats us
having to term serv into the web server and run the Process Explorer to kill
it there.
Mar 7 '06 #6
> The thread itself is created by a service process (a web server's
ISAPI that handles authentication). It makes some calls to a dll
(msvcrt.dll) and the info from ProcessExplorer shows it as
msvcrt.dll!endthreadex+0x2f.

Other threads that are spawned like this go right back to zero CPU,
and just go away. But sometimes they get stuck (with 25-50% of the
CPU) and just hang around. I've use the Process Explorer tool to
suspend and to kill the threads, and its had no detrimental effect on
the service. The web service call that generated the issue completes
successfully, and subsequent calls all work fine. That's what I meant
by 'while it was exiting'.

The reason we're looking at this is that these hanging threads can eat
up 100% CPU with just 2 or 4 of these calls hung. Not a perfect
reason to terminate the thread, but, while we're waiting on the
vendor, it beats us having to term serv into the web server and run
the Process Explorer to kill it there.


When you terminate a thread in a .NET application you should consider the
whole appdomain doomed, as in, you should no longer keep it around.

While you said it was not a thread in another .NET application, the comment
is no less appropriate.

If you terminate a thread from the outside (of the thread, could be in the
same process), then you risk the following:

- memory leak, the thread did not get to its cleanup code
- resource leak (open files, sockets, etc.), the thread did not get to is
cleanup code
- invalid application state, the thread did not finalize its changes to internal
variables and might leave the application in a state that is invalid
- deadlocks, if the thread locks internal resources it will never unlock
them, thus blocking future threads from ever accessing the resources the
lock protects

Considering this is a service I'd avoid this at all cost. If you absolutely
have to kill the thread, can you restart the service in question so that
its state is again valid?

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Mar 7 '06 #7

"Rob" <La*********@nospam.nospam> wrote in message
news:61**********************************@microsof t.com...
| The thread itself is created by a service process (a web server's ISAPI
that
| handles authentication). It makes some calls to a dll (msvcrt.dll) and
the
| info from ProcessExplorer shows it as msvcrt.dll!endthreadex+0x2f.
|
| Other threads that are spawned like this go right back to zero CPU, and
just
| go away. But sometimes they get stuck (with 25-50% of the CPU) and just
hang
| around. I've use the Process Explorer tool to suspend and to kill the
| threads, and its had no detrimental effect on the service. The web
service
| call that generated the issue completes successfully, and subsequent calls
| all work fine. That's what I meant by 'while it was exiting'.
|
| The reason we're looking at this is that these hanging threads can eat up
| 100% CPU with just 2 or 4 of these calls hung. Not a perfect reason to
| terminate the thread, but, while we're waiting on the vendor, it beats us
| having to term serv into the web server and run the Process Explorer to
kill
| it there.

I assume that both ISAPI and the DLL are implemented using pure native C++,
please correct me if I'm wrong, this is extremely important.
Ok assumed all is unmanaged, so you have a ISAPI DLL that runs a thread
procedure (from the third party DLL) and this thread procedure gets stuck
when it returns (that is when it calls _endthreadex as a result of the
return or explicitly). That means that your webrequest hangs (does not
progress) until you kill that thread, right?
Now what happens in _endthreadex is not something that would cause an
endless loop, so I doubt the explorer info is accurate, my guess is that
_endthreadex is actually running ThreadExit(..) which calls the attached
DLL's (all of them) DllMain function to indicate that the thread is
detaching from the DLL. So, I think your thread is looping in a (the third
party?) DLL's detach function, or you are kind of deadlocking on a detach
(DllMain can only be entered by one thread at a time!). The only thing you
can do to be sure of this is to attach a debugger (instead of explorer),
find out which thread is 'stuck and where (which DLL). Another thing you can
do is ask your vendor what he is doing in the DllMain detach function.
Again, it's very bad practice to kill such a thread, don't forget that you a
running inside IIS and that when killing a thread you will leak a thread
handle, the TLS and the stack assigned to the thread, so after some time
your IIS server will die taking all applications running inside the worker
processes with him, not a pretty picture.

Willy.


Mar 7 '06 #8
Hi,

Thanks for your reply!
"I'm not sure if it makes a difference, but its not a thread from another

..NET application."
No matter want kinds of the application, I think using the way which I
mentioned before is an only way to do this, if you can not obtain any fix
for the current issue.

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.
================================================== ====

Mar 8 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eric Marvets | last post by:
I want to make the main thread in my app sleep while I have a worker do some arbitrary task. The problem is waking the main thread back up. Can anyone make this code sample work? private void...
10
by: Jacek Pop³awski | last post by:
Hello. I am going to write python script which will read python command from socket, run it and return some values back to socket. My problem is, that I need some timeout. I need to say for...
2
by: Vitale Ferruccio | last post by:
Hi all, I've two threads: the former blocked in an sleep(seconds) and the latter which have to kill the former. Even if I call pthread_cancel or deliver a SIGTERM signal with pthread_kill, the...
2
by: Julien | last post by:
When trying to run this code, I get the following error message : Unhandled Exception: System.Runtime.InteropServices.COMException (0x80010106): Cannot change thread mode after it is set. 1....
15
by: WXS | last post by:
When I see things in .NET 2.0 like obsoletion of suspend/resume because of the public reason MS gives of they think people are using them inappropriately.. use mutex, monitor and other...
0
by: Maqbool | last post by:
Hi, I am intantiating thread that do more extensive work, i.e. executing query on FoxPro 2.6 through ODBCDataAdapter with VisualFoxPro driver. Since the query executing is Complex and navigating...
0
by: carmadamus | last post by:
I have to run a commandline tool and then parse its textoutput. Problem is the tool is pretty unreliable and often stops responding. It doesnt really crash just stops responding and doesnt exit. ...
2
by: Max | last post by:
Hello, I made an application that uses the main thread for the UI, and another thread to communicate through the RS232 port. I would like the communication thread to be suspended immediately when...
5
by: Uzi | last post by:
Hi all, I have a thread which is running a long method in a third party component (witch is a com object and accessed via interop). I need to be able to suspend this thread. the...
5
by: Mythran | last post by:
I've read some documentation about accessing objects created in other threads and the code below is similar to what I've gathered for what I have to do: private delegate void...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.