473,503 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
5 8973
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4829
by: Pieter | last post by:
Hi, I'm having some weird problem using the BackGroundWorker in an Outlook (2003) Add-In, with VB.NET 2005: I'm using the BackGroundWorker to get the info of some mailitems, and after each item...
2
8721
by: Sebastian Crewe | last post by:
Greetings, I was much encouraged to see the new BackgroundWorker class in .NET v2. On the face of it, much easier to use than the various delegates and events of yore, though I imagine the same...
5
14096
by: Rob R. Ainscough | last post by:
I'm using a BackgroundWorker to perform a file download from an ftp site. Per good code design practices where I separate my UI code from my core logic code (in this case my Download file method in...
1
3214
by: Bob | last post by:
Hi, I am having trouble seeing how this bolts together. The UI starts a process which involves a long running database update. All Database activity is handled by a class called DT. DT has a...
3
1775
by: Ben | last post by:
Hi We have a windows form that takes a while to run a routine. During this we have created a information label, updating the user on the progress but: a) The changes on the label are not...
5
11166
by: redear | last post by:
Is there a way to immediately terminate a BackgroundWorker thread? My problem is that the BackgroundWorker starts with a call to My.Computer.FileSystem.GetFiles that can run for a very long time if...
5
5537
by: Michael M. | last post by:
I have the following code (listed at bottom of post) that pings a small range of IP address to see which ones are alive. To speed things up a little I am trying to use more than one thread,...
9
18003
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
0
1063
Plater
by: Plater | last post by:
So it occured to me that I told people to use this to solve some thread-safeing issues, but that I had not been using it myself (I had been using a delegate with invokerequired which seemed to lock...
0
7198
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7072
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7319
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5570
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.