How to stop a BackgroundWorker thread that calls a C++ dll

T

Torben Laursen

Hi

My interface calls a C++ dll that runs a slow calculations.
So I call the dll inside a BackgroundWorker thread, and that all works fine.

But how can I stop the thread if the user wants to? I can not tell the dll
to stop so CancelAsync() will not work?

I need a way to simply kill the "thread" since nothing in it will be used if
the user wants to cancel

Thanks Torben
 
M

Mehdi

My interface calls a C++ dll that runs a slow calculations.
So I call the dll inside a BackgroundWorker thread, and that all works fine.

But how can I stop the thread if the user wants to? I can not tell the dll
to stop so CancelAsync() will not work?

I need a way to simply kill the "thread" since nothing in it will be used if
the user wants to cancel

As far as i know, you can't. You'll need to modify your C++ code to allow
for cancelation.
 
T

Torben Laursen

Thanks

Yes changing the C++ code would be the best solution, but that is not
possible for a number of reasons.

The dll is loaded inside a class that I create when I run the thread.

Is there a way to simply dispose that class thereby stopping whatever is
running inside the dll?
Or can I create a Thread inside a backgroundworking thread?

Torben
 
M

Martijn Boven

How long would this calculation last?
Are we talking about minutes, seconds?
 
M

Mehdi

Yes changing the C++ code would be the best solution, but that is not
possible for a number of reasons.

The dll is loaded inside a class that I create when I run the thread.

Is there a way to simply dispose that class thereby stopping whatever is
running inside the dll?

No. Unfortunately, diposing an object does not automagically kill all the
threads that happen to be executing one of its methods or accessing one of
its member variables.
Or can I create a Thread inside a backgroundworking thread?

Sure you can but it wouldn't make much sense.

What you could do would be to manually create your own thread to execute
your C++ method instead of using the BackgroundWorker. Then you would have
your thread and be able to do whatever you want to do with it. Problem is,
you still won't be able to stop your long running C++ method. The Thread
class has an Abort() method that is supposed to stop the thread. Usage of
this method is strongly discouraged for many reasons (you'll find loads of
discussions about why Thread.Abort() is/is not evil on google). But in your
particular case, i think that even if you went to the dark side and used
Thread.Abort(), it still wouldn't work since i believe (not a specialist
though) that Thread.Abort() is not able to interupt unmanaged code and will
wait until your code comes back to the managed world before stopping the
thread.

Of course, the OTT method to do what you want to do would simply be to
launch a new process and execute your method in this new process
communicating back to the main process via .NET Remoting or some other
means. Then, should you need to stop the operation, you'd be able to kill
the worker process (potentially creating resources leaks).
 
T

Torben Laursen

Hi

It takes between 0.001 s and more then one hour depending on the
calculation.

But based on Mehdi's feedback I think that I will try to find a way to
update the C++ dll, since in the long run it seems that it
will be a good investment of time.

Thanks
Torben
 

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