473,796 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BackgroundWorke r

I know I can stop my thread by using the CancelAsync method of the
BackgroundWorke r but that requires me to monitor the value of the
CancellationPen ding property in my BackgroundWorke r.DoWork event.

This means that if the DoWork event is processing a single line of code then
my code must wait for this line to finish before it can check the value of
CancellationPen ding.

What if the user wants to cancel before I can check this value? Is there any
way to outright kill the thread, immediately without monitoring any
properties within DoWork?

TIA
May 19 '06 #1
5 2701
What are your specific requirements? IOW, what business requirement is this
solution supposed to satisfy?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"elziko" <el****@yahoo.c o.uk> wrote in message
news:%2******** *******@TK2MSFT NGP02.phx.gbl.. .
I know I can stop my thread by using the CancelAsync method of the
BackgroundWork er but that requires me to monitor the value of the
CancellationPe nding property in my BackgroundWorke r.DoWork event.

This means that if the DoWork event is processing a single line of code
then my code must wait for this line to finish before it can check the
value of CancellationPen ding.

What if the user wants to cancel before I can check this value? Is there
any way to outright kill the thread, immediately without monitoring any
properties within DoWork?

TIA

May 19 '06 #2
AFAIK terminating a thread by using brute force (Abort) is considered as a
bad practice.

Executing few lines of code should be really unoticeable to your user
anyway. If you found an issue with your current approach, I would suggest
posting first about this issue instead of already choosing another
approach...

--

"elziko" <el****@yahoo.c o.uk> a écrit dans le message de news:
%2************* **@TK2MSFTNGP02 .phx.gbl...
I know I can stop my thread by using the CancelAsync method of the
BackgroundWork er but that requires me to monitor the value of the
CancellationPe nding property in my BackgroundWorke r.DoWork event.

This means that if the DoWork event is processing a single line of code
then my code must wait for this line to finish before it can check the
value of CancellationPen ding.

What if the user wants to cancel before I can check this value? Is there
any way to outright kill the thread, immediately without monitoring any
properties within DoWork?

TIA

May 19 '06 #3
Patrice wrote:
Executing few lines of code should be really unoticeable to your user
anyway.


OK, heres a bit more background on what I'm doing:

I have a requirement to create a netCDF file that contains two records of
data. You're probably not aware of netCDF files but they are commonly used
to store scientific data.

In one line of code in my thread I call a method in a COM DLL that provides
netCDF support that creates a netCDF file capable of storing our data. This
is basically a four dimensional array of size 150 x 150 x150 x 500 as an
example or a record. It is this single line that sometimes takes several
minutes to execute and I'd like the user to be able to cancel during this
time.

Perhaps a few lines of code shouldn't really be noticeable but that depends
on what each line does! Especially when a line of code is just a call to
someone elses code that I have no control over. I don't see how it could be
designed differently. I must use the netCDF format and the COM DLL that I
use to access netCDF files is not something I can change.

I hope that makes things a bit clearer!

TIA
May 19 '06 #4
If you abort the thread during this, I'm afraid it could left the whole
stuff in an unpredictable state (or have you checked that this library
doesn't have support for doing this).

Another strategy might be to let the thread continue and to launch another
thread if the user wants to perform some other background task.

Also the likelyness the user will want to abort may vary with the
significance of this operation (i..e it's more likely you'll want to cancel
an everyday computation if you remember your forgot to enter data rather
than a DB creation operation that you lauch every few months really when you
need it).

--
Patrice

"elziko" <el****@yahoo.c o.uk> a écrit dans le message de news:
%2************* ***@TK2MSFTNGP0 5.phx.gbl...
Patrice wrote:
Executing few lines of code should be really unoticeable to your user
anyway.


OK, heres a bit more background on what I'm doing:

I have a requirement to create a netCDF file that contains two records of
data. You're probably not aware of netCDF files but they are commonly used
to store scientific data.

In one line of code in my thread I call a method in a COM DLL that
provides netCDF support that creates a netCDF file capable of storing our
data. This is basically a four dimensional array of size 150 x 150 x150 x
500 as an example or a record. It is this single line that sometimes takes
several minutes to execute and I'd like the user to be able to cancel
during this time.

Perhaps a few lines of code shouldn't really be noticeable but that
depends on what each line does! Especially when a line of code is just a
call to someone elses code that I have no control over. I don't see how it
could be designed differently. I must use the netCDF format and the COM
DLL that I use to access netCDF files is not something I can change.

I hope that makes things a bit clearer!

TIA

May 19 '06 #5
I don't think you have much of a choice here. The COM DLL is a black box,
and you don't know what will happen if you abort the thread while it is
running a task. Of course, since it's a background thread, all you really
need to do is have it delete the file afterwards if the user cancels. They
should not experience any change, because it's a background thread. Unless I
misunderstand you.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"elziko" <el****@yahoo.c o.uk> wrote in message
news:%2******** ********@TK2MSF TNGP05.phx.gbl. ..
Patrice wrote:
Executing few lines of code should be really unoticeable to your user
anyway.


OK, heres a bit more background on what I'm doing:

I have a requirement to create a netCDF file that contains two records of
data. You're probably not aware of netCDF files but they are commonly used
to store scientific data.

In one line of code in my thread I call a method in a COM DLL that
provides netCDF support that creates a netCDF file capable of storing our
data. This is basically a four dimensional array of size 150 x 150 x150 x
500 as an example or a record. It is this single line that sometimes takes
several minutes to execute and I'd like the user to be able to cancel
during this time.

Perhaps a few lines of code shouldn't really be noticeable but that
depends on what each line does! Especially when a line of code is just a
call to someone elses code that I have no control over. I don't see how it
could be designed differently. I must use the netCDF format and the COM
DLL that I use to access netCDF files is not something I can change.

I hope that makes things a bit clearer!

TIA

May 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9278
by: dm1608 | last post by:
Hi -- I have a C# application that basically has a button that executes a SQL Reader to loop thru a rather large resul set. Thru each interation of the reader object, I check to see if a file exists on the server. If it does, I update a textbox (multiline) with the missing file, update a status bar label, and a progressbar. Since this process is kind of lengthy, every 400 rows, I do a DoEvents() and TextBox.Refersh() to refresh the...
5
14134
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 my FileIO class) I've established Public Event in my core logic classes along with RaiseEvents (that will updated a progress bar on the UI side). This all works great when I'm NOT using Threading (BackgroundWorker), however, as soon as I...
1
3235
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 progress event. So I added a bw to the form. The Dowork Calls a method which instantiates a DT and calls its Dataprocessing method.
5
11237
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 it is pointing to a directory tree with many files. If the user requests cancellation during this time, the main thread can call CancelAsync and can post a "Cancellation Pending" message to the user, but the BackgroundWorker cannot respond to...
14
6396
by: =?Utf-8?B?SXNobWFlbA==?= | last post by:
Hi, I have a form with a progress bar on it and wanted to use the BackgroundWorker to be able to update the progress. I looked at examples, run some of them, but in debug, when the code gets to the do work method, it stops for a long while and then executes the first line of the method and the doesn't do anything else. What am i doing wrong?
0
2482
by: Chris | last post by:
I would like to be able to pass the BackgroundWorker object and DoWork Event Args to a second function (third function?) and be able to still report the progress. I'm getting the following exception when trying to access BackgroundWorker.CancellationPending or e.Cancel An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll Additional information: Exception has been thrown by the target of...
2
4840
by: Chris | last post by:
When I try to access the backgroundWorker.CancellationPending property I get the following exception: An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll Additional information: Exception has been thrown by the target of an invocation. I am using the backgroundWorker in a component that I am trying to make for doing encryption of large files in the background.
9
18036
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 will almost always lead to the user opening a certain child window that's fairly resource intensive to load. Is it possible to load this form in a backgroundworker and then use the Show method and hide method as necessary? Anyone know of
4
2584
by: Sin Jeong-hun | last post by:
This is what I've always been wondered. Suppose I've created a class named Agent, and the Agent does some lengthy job. Of course I don't want to block the main window, so the Agent does the job in a separate thread. If the job is progressed it fires an event, and the main window handled the event by changing the value of a progress bar. The problem is that this event is fired in another thread so when the handler in the main window tries...
2
11872
by: =?Utf-8?B?SGFycnkgS2Vjaw==?= | last post by:
I have introduced a component to my solution that is throwing an exception about needing to be run in single threaded apartment mode. This component is created in an async call by a BackgroundWorker object, which seems to be on a MTA thread. Is there a way for me to get the BackgroundWorker thread to run in STA mode, or do I have to remove the BackgroundWorker and hand spin my own async call?
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10242
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10021
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6800
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5453
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.