473,665 Members | 2,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BackgroundWorke r and incremental results

How do I implement the "incrementa l results" with a BackgroundWorke r?
Many pages,

http://msdn2.microsoft.com/en-us/library/wewwczdw.aspx

for example, tell that I should subclass the ProgressChanged EventArgs
class, but what else? I haven't found any examples. (Why aren't there
any?) Right now I have derived my own version of
ProgressChanged EventArgs with some extra properties, declared

public delegate void FilterWorkerPro gressChanged(Ob ject sender,
FilterProgressC hangedEventArgs e);
public event FilterWorkerPro gressChanged FilterProgressC hanged;

in the publisher class and added a handler for this event here:

filterWorker.Fi lterProgressCha nged +=
progDialog.filt erWorker_Progre ssChanged;

Of course, right now calling ReportProgress doesn't do anything
because it will raise a ProgressChanged event, which is unhandled. How
do I raise that FilterProgressC hanged event or do I even need to make
my own event for this? I guess one way would be to subclass
BackgroundWorke r, but that CANNOT be the easiest way to do it. And
then I would have to guess what to write to those overridden
ReportProgress, OnProgressChang ed, etc., not good.

A simple (but rigorous) example would be the best answer. I just
started learning C# like two days ago (I like it thus far), so its
event handling model (or paradigm....... ..) is still a bit mystery to
me.

Thanks!

Jul 21 '07 #1
3 4818
Unfortunately, subclassing the BackgroundWorke r class is exactly what
you have to do. There is no way to fire the event you want without using a
subclass (which would require you to override the OnProgressChang ed method
to fire your event).

You could always use the overload of ReportProgress which takes a
parameter of type Object, but if your needs dictate that the specialization
is necessary, then subclasisng is the only way to go.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"vulpes" <ni*********@tk k.fiwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.com.. .
How do I implement the "incrementa l results" with a BackgroundWorke r?
Many pages,

http://msdn2.microsoft.com/en-us/library/wewwczdw.aspx

for example, tell that I should subclass the ProgressChanged EventArgs
class, but what else? I haven't found any examples. (Why aren't there
any?) Right now I have derived my own version of
ProgressChanged EventArgs with some extra properties, declared

public delegate void FilterWorkerPro gressChanged(Ob ject sender,
FilterProgressC hangedEventArgs e);
public event FilterWorkerPro gressChanged FilterProgressC hanged;

in the publisher class and added a handler for this event here:

filterWorker.Fi lterProgressCha nged +=
progDialog.filt erWorker_Progre ssChanged;

Of course, right now calling ReportProgress doesn't do anything
because it will raise a ProgressChanged event, which is unhandled. How
do I raise that FilterProgressC hanged event or do I even need to make
my own event for this? I guess one way would be to subclass
BackgroundWorke r, but that CANNOT be the easiest way to do it. And
then I would have to guess what to write to those overridden
ReportProgress, OnProgressChang ed, etc., not good.

A simple (but rigorous) example would be the best answer. I just
started learning C# like two days ago (I like it thus far), so its
event handling model (or paradigm....... ..) is still a bit mystery to
me.

Thanks!
Jul 21 '07 #2
Gaz
"vulpes" <ni*********@tk k.fiwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.com.. .
>How do I implement the "incrementa l results" with a BackgroundWorke r?
Many pages,

http://msdn2.microsoft.com/en-us/library/wewwczdw.aspx

for example, tell that I should subclass the ProgressChanged EventArgs
class, but what else? I haven't found any examples. (Why aren't there
any?) Right now I have derived my own version of
ProgressChange dEventArgs with some extra properties, declared

public delegate void FilterWorkerPro gressChanged(Ob ject sender,
FilterProgress ChangedEventArg s e);
public event FilterWorkerPro gressChanged FilterProgressC hanged;

in the publisher class and added a handler for this event here:

filterWorker.F ilterProgressCh anged +=
progDialog.fil terWorker_Progr essChanged;

Of course, right now calling ReportProgress doesn't do anything
because it will raise a ProgressChanged event, which is unhandled. How
do I raise that FilterProgressC hanged event or do I even need to make
my own event for this? I guess one way would be to subclass
BackgroundWork er, but that CANNOT be the easiest way to do it. And
then I would have to guess what to write to those overridden
ReportProgress , OnProgressChang ed, etc., not good.

A simple (but rigorous) example would be the best answer. I just
started learning C# like two days ago (I like it thus far), so its
event handling model (or paradigm....... ..) is still a bit mystery to
me.

Thanks!
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:6B******** *************** ***********@mic rosoft.com...
Unfortunately, subclassing the BackgroundWorke r class is exactly what
you have to do. There is no way to fire the event you want without using
a subclass (which would require you to override the OnProgressChang ed
method to fire your event).

You could always use the overload of ReportProgress which takes a
parameter of type Object, but if your needs dictate that the
specialization is necessary, then subclasisng is the only way to go.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

Can't you do it this way?

Call worker.ReportPr ogress from within DoWork and pass some state object
thus:

private void worker_DoWork(o bject sender, DoWorkEventArgs e)
{
...
worker.ReportPr ogress(progress Percentage, state); // state
contains incremental results
...
}

private void worker_Progress Changed(object sender, ProgressChanged EventArgs
e)
{
// Now we are in the main form thread
// e.UserState will hold our state object, cast it back and use it as
you will
// Or raise your own custom event from here
OnMyCustomEvent (this, new MyCustomEventAr gs(...))
}

private void OnMyCustomEvent (object sender, MyCustomEventAr gs e)
{
if (MyCustomEvent != null)
MyCustomEvent(s ender, e)
}
}

This is the way I am doing it atm.
Gaz
Jul 21 '07 #3
On Jul 21, 5:45 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
You could always use the overload of ReportProgress which takes a
parameter of type Object, but if your needs dictate that the specialization
is necessary, then subclasisng is the only way to go.
But I understand that using the userState object is exactly what you
must NOT do in this situation. The MSDN Docs always talk about using
the userState strictly as an ID for the worker and you will burn in
hell (deadlocked there...) if you use it for some other thing (for
example change it during the run). On the other hand I've seen many
people in the net talk about using it and then wondering why it
doesn't work. So if you're really gonna tell me that I can do that
then please, say it again.

Jul 21 '07 #4

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

Similar topics

2
7386
by: Matthias S. | last post by:
Hi, I've written a simple app which should just fetch some data from a database and render the results into a ListView. In order to not freeze the GUI, I'm using a BackgroundWorker. The arguments for the RunWorkerAsync are the Query to be executed and the Connectionstring. As a result I hoped I could return a SqlDataReader. The problem is, if I create the SqlDataReader within the
3
6164
by: Tim Anderson | last post by:
I've been experimenting with the BackgroundWorker class. As a test, I've built an application that searches for files matching a RegEx and populates a listbox with the results. My approach is to search directories recursviely andcall ReportProgress once per directory or in batches of 10 matching files if there are a lot of results. I pass an ArrayList containing the matching filenames. In the ProgressChanged event handler I add the...
1
22071
yabansu
by: yabansu | last post by:
Hi all, I implemented a basic client application. It communicates with server and works properly. Building processes succeed just by giving the following warning: LINK : D:\DOC\Visual Studio 2005\Projects\Client01\Debug/Client01.exe not found or not built by the last incremental link; performing full link This warning occurs when building/rebuilding the project after cleaning the intermediate and output files. If the project is once...
14
6383
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?
35
3018
by: mwelsh1118 | last post by:
Why doesn't C# allow incremental compilation like Java? Specifically, in Java I can compile single .java files in isolation. The resulting individual .class files can be grouped into .jar files. In C#, there appears to be no analog. I have to compile all my .cs files into a single .dll. This has serious drawbacks in terms of compilation. With Eclipse, I change a file and only that file is re-compiled. With Visual Studio, I
1
2329
by: nondos | last post by:
Is it possible to suspend BackGroundWorker Control like it's possible in thread? Thanks
2
1856
by: csharpula csharp | last post by:
Hello, I would like to know how can I fire events of background worker when I want to let know the main thread that the action finished and to send results. Is there a way to fire RunWorkerCompletedEventArgs in events from background to main thread? Thank you
1
1865
by: Maxwell | last post by:
So, I am building a class which implements the Event-based Asynchronous pattern. We'll call it MyClass. Suppose part of it looks like the following: /// <code> public class MyClass : Component { private BackgroundWorker worker; public void MyMethodAsync()
7
4800
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
How do you cancel a BackgroundWorker? For this BackgroundWorker: bgWorker.WorkerReportsProgress = true; bgWorker.WorkerSupportsCancellation = true; I have tried this, but it never exits the do...while loop: if (bgWorker.IsBusy == true) {
1
8549
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
8636
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...
0
7376
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
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
4186
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...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
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
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.