Aborting an STA thread doesn't work

M

Michel

Hi there,

I have a thread pool that contains a number of threads waiting for jobs to
process.
The logic for stopping a thread is that I check if the thread is in waiting
mode, if so, I abort it.
The run method of the thread is basically this:

while(!IsAborted)
try {
JobQueue.Pop().Execute(); // If there no jobs, Pop() enters Monitor.Wait
} catch (ThreadAbortException) {
IsAborted = true;
Thread.ResetAbort();
}

This works fine as long as I don't change the ApartmentState to
ApartmentState.STA. When I change it to STA, I never get to the
ThreadAbortException block.

Any ideas appreciated!

Thanks,
Michel
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Michael,

Frankly, I don't know what your problem might be. Without working example
only a person that has had this very problem could help you. So in order to
get more help pls post some simple working program that demonstrates the
problem.
Anyways, bare in mind that thread apartment can be set only once for a
thread. If you set the apartment once for a thread any other changing won't
have any effect. Secondly STA threads need to have message pump running
otherwise they cannot serve the purpuse of being single appartment threads.
Normaly worker threads doesn't have message queue so I don't know how it
make sense to have them STA
 
M

Michel

Hi Stoitcho,

I know about only setting the apartment once. I do this before I start the
threads.
And I prefer to have MTA but I have another problem with a dll that won't
run in MTA.
(the threads are calling a range of dlls, com objects and soap services)
I'm trying to solve this, but if I can't I'll have to use STA.

Maybe I'll post a code sample but this will be difficult since it's a lot of
code and I can't post the objects I'm actually calling in the threads (they
are from several parties).

Thanks for the suggestion.
Michel

Stoitcho Goutsev (100) said:
Hi Michael,

Frankly, I don't know what your problem might be. Without working example
only a person that has had this very problem could help you. So in order to
get more help pls post some simple working program that demonstrates the
problem.
Anyways, bare in mind that thread apartment can be set only once for a
thread. If you set the apartment once for a thread any other changing won't
have any effect. Secondly STA threads need to have message pump running
otherwise they cannot serve the purpuse of being single appartment threads.
Normaly worker threads doesn't have message queue so I don't know how it
make sense to have them STA

--

Stoitcho Goutsev (100) [C# MVP]


Michel said:
Hi there,

I have a thread pool that contains a number of threads waiting for jobs to
process.
The logic for stopping a thread is that I check if the thread is in waiting
mode, if so, I abort it.
The run method of the thread is basically this:

while(!IsAborted)
try {
JobQueue.Pop().Execute(); // If there no jobs, Pop() enters Monitor.Wait
} catch (ThreadAbortException) {
IsAborted = true;
Thread.ResetAbort();
}

This works fine as long as I don't change the ApartmentState to
ApartmentState.STA. When I change it to STA, I never get to the
ThreadAbortException block.

Any ideas appreciated!

Thanks,
Michel
 
D

David Levine

If the thread is executing unmanaged code then the abort wont be delivered
to the thread until it returns to managed code. In other words, if you are
doing interop to a COM object the abort wont be seen until control returns
to managed code.
 
M

Michel

Hi David,

I know, and I only abort if the thread is waiting for the queue. Otherwise I
let the unmanaged code finish.

Thanks for the suggestion,
Michel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top