By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,281 Members | 2,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,281 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 4 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Tue, 4 Jul 2006 11:29:43 +0200, Torben Laursen wrote:
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.
Jul 4 '06 #2

P: n/a
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

"Mehdi" <vi****@REMOVEME.gmail.comwrote in message
news:ac*****************************@40tude.net...
On Tue, 4 Jul 2006 11:29:43 +0200, Torben Laursen wrote:
>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.

Jul 4 '06 #3

P: n/a
How long would this calculation last?
Are we talking about minutes, seconds?
"Torben Laursen" <To****@newsgroups.nospamwrote in message
news:OR**************@TK2MSFTNGP02.phx.gbl...
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

Jul 4 '06 #4

P: n/a
On Tue, 4 Jul 2006 12:40:31 +0200, Torben Laursen wrote:
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).
Jul 4 '06 #5

P: n/a
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

"Martijn Boven" <ma*****@daxis.nlwrote in message
news:u7**************@TK2MSFTNGP04.phx.gbl...
How long would this calculation last?
Are we talking about minutes, seconds?
"Torben Laursen" <To****@newsgroups.nospamwrote in message
news:OR**************@TK2MSFTNGP02.phx.gbl...
>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


Jul 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.