What is normal practice of stopping threads that do long operations?

137 Views Asked by At

Lets say I am about to close application/stop service, but one of my threads is going a long operation that I have no control of (lets say thirty party component is used). What is normal practice of stopping threads that do long operations? Windows service doesn't like long operations (when I click Stop button).

1

There are 1 best solutions below

4
Solomon Slow On

one of my threads is going a long operation that I have no control of (lets say third party component is used).

That's a deep problem. You are asking for a world of trouble if your threads don't cooperate with each other. The problem with killing a thread is, you have no way of knowing what the thread was doing at the moment it was killed. You could kill it while it was in the middle of allocating an object from the heap, and that could leave the heap corrupted. Your program soon would crash. You could kill it while some mutex was locked, and the next thread that tried to lock the same mutex would hang.

IMO: the best way out of the box you're in would be to perform that "long operation" in a separate process. It's much easier to design a process that can be safely killed.