473,382 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Using BackgroundWorker

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 matching filenames to a listbox.

When I first ran the app, and if I use a blank RegEx matching all files, I
was surprised to find the UI frozen during the search just as if I were not
using BackgroundWorker. I've found I can fix this by inserting a call to
Thread.Sleep(1) just after I call ReportProgress.

Can someone explain why this is so?

Tim
Nov 21 '05 #1
3 6147
The problem is adding the items to the listbox. Since gui controls are not
thread-safe this needs to be done on the main thread. By adding a sleep you
give the main thread the change to do other events.

Gabriel Lozano-Morán

"Tim Anderson" <ti*****@hotmail.com> wrote in message
news:ug****************@TK2MSFTNGP14.phx.gbl...
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 matching filenames to a listbox.

When I first ran the app, and if I use a blank RegEx matching all files, I
was surprised to find the UI frozen during the search just as if I were
not using BackgroundWorker. I've found I can fix this by inserting a call
to Thread.Sleep(1) just after I call ReportProgress.

Can someone explain why this is so?

Tim

Nov 21 '05 #2
"Gabriel Lozano-Morán" <ga*****@no-spam.org> wrote in message
news:Og*************@TK2MSFTNGP11.phx.gbl...
The problem is adding the items to the listbox. Since gui controls are not
thread-safe this needs to be done on the main thread. By adding a sleep
you give the main thread the change to do other events.


I don't think it is quite that simple. I've done a bit more investigation.

The problem in a nutshell is that if you are (for example) matching every
file, then updating the GUI takes more time than doing the actual search.
ReportProgress is actually an async method, so what happens is that the next
call to ReportProgress comes before the GUI thread has actually finished
updating the GUI. The framework seems to sort this out so the app doesn't
actually crash, but the result is that the GUI thread is continuously busy
in the ProgressChanged event handler. The effect is a frozen UI.

I tried using the old Form.BeginInvoke approach instead of the
ReportProgress method. This has the same problem if I omit the call to
Form.EndInvoke - the GUI freezes. However, if I include a call to EndInvoke
in my GUI update method, it solves the problem. The GUI stays responsive,
and there's no need to call Thread.Sleep. I don't know if there's a good way
to do this using ReportProgress. Comments welcome.

Tim
Nov 23 '05 #3
"Tim Anderson" <ti*****@hotmail.com> wrote in message
news:eP*************@TK2MSFTNGP15.phx.gbl...
"Gabriel Lozano-Morán" <ga*****@no-spam.org> wrote in message
news:Og*************@TK2MSFTNGP11.phx.gbl... and there's no need to call Thread.Sleep. I don't know if there's a good
way to do this using ReportProgress. Comments welcome.


For example, I tried using a Semaphore to force ReportProgress to wait until
the ProgressChanged handler completed. This works fine too.

Tim
Nov 23 '05 #4

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

Similar topics

2
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...
3
by: Pro1712 | last post by:
Hi, this may be a stupid question: How can I can call the DoWork-function of a BackgroundWorker synchronous? Or in other words: How can I extend the BackgroundWorker class with a function...
4
by: DHarry | last post by:
Can anybody tell me, whats the main difference between a BackgroundWorker and a Thread? What are the advantages/disadvanteges? When use a BackgroundWorker, when a Thread?
11
by: GVN | last post by:
Hi All, Can anyone guide me when asynchronous method calls will be benificial? Are there any disadvantages of using asynchronous calls? Thanks,
1
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...
5
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
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,...
0
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...
2
by: Peter Carlson | last post by:
I am testing backgroundWorker to update our installer with both messages and with a progress bar. I am finding however that the text never gets updated. Any ideas what I might be doing wrong? ...
0
by: PeterSchwennesen | last post by:
Problems starting a Timer Programmatically within a BackgroundWorker. I am trying to start a Timer inside a Backgroundworker. I want to start the BackGroundWorker and then have a timer tick a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.