473,662 Members | 2,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BackgroundWorke r and events handling

Hi all,
I am migrating a Windows Form application from .Net 1.1 to 2.0. I try to
use BackgroundWorke r object to handle a very lengthy process.
I have a separated class to handle some very complex logic. In .Net 1.1,
I create some events inside this class to notify WinForm the status of
process:

obj.OneFileStar ted += new OneFileStartedE ventHandler(obj _OneFileStarted );
obj.OneFileFini shed += new OneFileFinished EventHandler(ob j_OneFileFinish ed);
obj.AllFinished += new AllFinishedEven tHandler(obj_Al lFinished);
obj.ErrorHappen ed += new ErrorHappenedEv entHandler(obj_ ErrorHappened);
.......
System.Threadin g.Thread t = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(o bj.LengthyJob)) ;
t.Start();

Then in my WinForm class, I just hook these events to run the UI content
change based on status/progress of object processing method.

In new BackgroundWorke r model, what is the proper way to notify UI of
different statuses/events? Because I only see a ReportProgress method which
can talk with outside world. I need more than this. If I don't make any
change, still use old way to receive events, I get "Cross-thread operation
not valid: Control 'labelFileName' accessed from a thread other than the
thread it was created on." exception, even though I change values of
Controls inside my event handler methods of WinForm.

private void obj_OneFileStar ted(object sender, ProcessFileArgs e) {
// I have exception here: Cross-thread operation not valid: Control
'labelFileName' accessed from a thread other than the thread it was created
on.
labelFileName.T ext = e.FileName;
}

--
WWW: http://www.imagestation.com/members/hardywang
ICQ: 3359839
yours Hardy
Oct 2 '06 #1
3 3045
Hardy,

It looks like you were being bad in .NET 1.1. In .NET 1.1, the Control
class didn't check to see if it's members were accessing it on the proper
thread. In .NET 2.0, they have changed this.

If you want to use the BackgroundWorke r class, then in your operation,
when you call the ReportProgress event, you want to pass an object for the
userState parameter. This is what you will use to indicate what you should
do in the event handler that will process the event on the UI thread.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Hardy Wang" <ha*******@news groups.nospamwr ote in message
news:%2******** *******@TK2MSFT NGP05.phx.gbl.. .
Hi all,
I am migrating a Windows Form application from .Net 1.1 to 2.0. I try
to use BackgroundWorke r object to handle a very lengthy process.
I have a separated class to handle some very complex logic. In .Net
1.1, I create some events inside this class to notify WinForm the status
of process:

obj.OneFileStar ted += new OneFileStartedE ventHandler(obj _OneFileStarted );
obj.OneFileFini shed += new
OneFileFinished EventHandler(ob j_OneFileFinish ed);
obj.AllFinished += new AllFinishedEven tHandler(obj_Al lFinished);
obj.ErrorHappen ed += new ErrorHappenedEv entHandler(obj_ ErrorHappened);
......
System.Threadin g.Thread t = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(o bj.LengthyJob)) ;
t.Start();

Then in my WinForm class, I just hook these events to run the UI
content change based on status/progress of object processing method.

In new BackgroundWorke r model, what is the proper way to notify UI of
different statuses/events? Because I only see a ReportProgress method
which can talk with outside world. I need more than this. If I don't make
any change, still use old way to receive events, I get "Cross-thread
operation not valid: Control 'labelFileName' accessed from a thread other
than the thread it was created on." exception, even though I change values
of Controls inside my event handler methods of WinForm.

private void obj_OneFileStar ted(object sender, ProcessFileArgs e) {
// I have exception here: Cross-thread operation not valid: Control
'labelFileName' accessed from a thread other than the thread it was
created on.
labelFileName.T ext = e.FileName;
}

--
WWW: http://www.imagestation.com/members/hardywang
ICQ: 3359839
yours Hardy

Oct 2 '06 #2
Hi Hardy,

I have added a reply to you for this issue in
microsoft.publi c.dotnet.framew ork.clr newsgroup, please check it there.
Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 3 '06 #3
Thanks a lot!

"Nicholas Paldino [.NET/C# MVP]" wrote:
Hardy,

It looks like you were being bad in .NET 1.1. In .NET 1.1, the Control
class didn't check to see if it's members were accessing it on the proper
thread. In .NET 2.0, they have changed this.

If you want to use the BackgroundWorke r class, then in your operation,
when you call the ReportProgress event, you want to pass an object for the
userState parameter. This is what you will use to indicate what you should
do in the event handler that will process the event on the UI thread.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Hardy Wang" <ha*******@news groups.nospamwr ote in message
news:%2******** *******@TK2MSFT NGP05.phx.gbl.. .
Hi all,
I am migrating a Windows Form application from .Net 1.1 to 2.0. I try
to use BackgroundWorke r object to handle a very lengthy process.
I have a separated class to handle some very complex logic. In .Net
1.1, I create some events inside this class to notify WinForm the status
of process:

obj.OneFileStar ted += new OneFileStartedE ventHandler(obj _OneFileStarted );
obj.OneFileFini shed += new
OneFileFinished EventHandler(ob j_OneFileFinish ed);
obj.AllFinished += new AllFinishedEven tHandler(obj_Al lFinished);
obj.ErrorHappen ed += new ErrorHappenedEv entHandler(obj_ ErrorHappened);
......
System.Threadin g.Thread t = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(o bj.LengthyJob)) ;
t.Start();

Then in my WinForm class, I just hook these events to run the UI
content change based on status/progress of object processing method.

In new BackgroundWorke r model, what is the proper way to notify UI of
different statuses/events? Because I only see a ReportProgress method
which can talk with outside world. I need more than this. If I don't make
any change, still use old way to receive events, I get "Cross-thread
operation not valid: Control 'labelFileName' accessed from a thread other
than the thread it was created on." exception, even though I change values
of Controls inside my event handler methods of WinForm.

private void obj_OneFileStar ted(object sender, ProcessFileArgs e) {
// I have exception here: Cross-thread operation not valid: Control
'labelFileName' accessed from a thread other than the thread it was
created on.
labelFileName.T ext = e.FileName;
}

--
WWW: http://www.imagestation.com/members/hardywang
ICQ: 3359839
yours Hardy


Oct 3 '06 #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
2
9269
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...
2
8736
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 base classes are being invoked. Anyway, I have given it a try with a VB.NET application that generates charts. Since there are some 3,000 charts to do, I wanted to put the chart generation on a separate thread. Works fine inasmuch as the...
5
14125
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...
2
3027
by: Simon Tamman | last post by:
Just wondering if anyone knows how this component works. If you set it up in the context of an Application.Run it works on it's worker thread but it updates the progress on the UI thread. My question is, how does it know which thread to use? I used Reflector to track down the fact that it gets the delegates to call via base.Events which derives from Component but how does it know what to put into base.Events? I'm very happy the...
3
4818
by: vulpes | last post by:
How do I implement the "incremental results" with a BackgroundWorker? Many pages, http://msdn2.microsoft.com/en-us/library/wewwczdw.aspx for example, tell that I should subclass the ProgressChangedEventArgs class, but what else? I haven't found any examples. (Why aren't there any?) Right now I have derived my own version of ProgressChangedEventArgs with some extra properties, declared
4
2578
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
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
4787
by: schnandr | last post by:
Hi, I have a user control which contains ListView. I copy all ListViewItems into a separate List<ListViewItemcalled unfilteredItems. I am using a BackgroundWorker to filter the ListView. When I access this list in the DoWork Handler I am getting an InvalidOperationException that the List was not created by this thread. Is there a way to solve this problem?
0
8435
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
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8547
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
7368
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
6186
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
5655
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
4181
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...
2
1754
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.