Controlling "grandchild" threads

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My program makes use of a third party class library to do some
transformations. The transformations take a long time, so I spawn the library
call as a new thread. The problem is that the library spawns threads of its
own, outside of my namespace. One of their threads is spawned at Highest
priority, causing the GUI to stop responding (defeating the whole purpose!).
Also, if I decide to cancel the operation and call Abort(), any new threads
continue on in perpetuity. That's obviously a problem.

I have no control over the class library, but I'd still like to enforce the
priority and abort the threads if I need to. Is there any way for my code to
even access these "grandchildren"?
 
Get yourself a new library - this one appears to have been written by a lunatic.

Unfortunately I don't think there is a way to get to these threads if the library doesn't expose a hook to you. By the way, you shouldn't call Abort on threads, it will probably bring the threads down but it may destabilize your AppDomain.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

My program makes use of a third party class library to do some
transformations. The transformations take a long time, so I spawn the library
call as a new thread. The problem is that the library spawns threads of its
own, outside of my namespace. One of their threads is spawned at Highest
priority, causing the GUI to stop responding (defeating the whole purpose!).
Also, if I decide to cancel the operation and call Abort(), any new threads
continue on in perpetuity. That's obviously a problem.

I have no control over the class library, but I'd still like to enforce the
priority and abort the threads if I need to. Is there any way for my code to
even access these "grandchildren"?
 
Actually, I guess you could enumerate the threads for the process by PInvoke to Thread32First and Thread32Next then look for high priority threads and reduce the priority but then you'll reduce the priority of the Finalizer thread which wouldn't be a good thing. But I don't think you can do this from managed code.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

My program makes use of a third party class library to do some
transformations. The transformations take a long time, so I spawn the library
call as a new thread. The problem is that the library spawns threads of its
own, outside of my namespace. One of their threads is spawned at Highest
priority, causing the GUI to stop responding (defeating the whole purpose!).
Also, if I decide to cancel the operation and call Abort(), any new threads
continue on in perpetuity. That's obviously a problem.

I have no control over the class library, but I'd still like to enforce the
priority and abort the threads if I need to. Is there any way for my code to
even access these "grandchildren"?
 
If you need to bring down a thread you should preferably set a flag that the thread monitors and let the thread bring itself down naturally. However, its possible the thread is in a state where it cannot check a flag (e.g. Thread.Sleep(), WaitHandle.WaitOne(), etc). In this case use Thread.Interrupt which will throw an exception on the thread if it is in an alertable state (so in other words it will only throw an exception on hte thread at defined points in time). Thread.Abort can throw at any time, even in a finally block (although this particular behaviour has now been fixed in v2.0)

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Other issues aside, if I'm not supposed to use Abort, what should I use?
 
Back
Top