473,472 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Background worker tasks sequentially?

I'm able to use the Bacground Worker class to execute task and pain the
UI. However I want to be able to execute n tasks in order, say n1, then
n2 if there were no errors during n1, n3 so on.

I'm wondering what would be the best way to do this , do I wait in a
do...while loop for each to finish and then start another ?

thanks
Sunit

Dec 13 '06 #1
6 5943
Sunit,

That's basically what you will have to do. It seems like you have three
tasks, but you want all three to run sequentially, just not on the UI
thread. You can still use the BackgroundWorker class for this, you just
have to do the first task with it, then the second, then the third, and so
on, and so on.

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

"sjoshi" <sj****@ingr.comwrote in message
news:11*********************@t46g2000cwa.googlegro ups.com...
I'm able to use the Bacground Worker class to execute task and pain the
UI. However I want to be able to execute n tasks in order, say n1, then
n2 if there were no errors during n1, n3 so on.

I'm wondering what would be the best way to do this , do I wait in a
do...while loop for each to finish and then start another ?

thanks
Sunit

Dec 14 '06 #2
Sunit,

In addition to what Nicholas said there is an event on the
BackgroundWorker class that is raised when the task is complete. You
should be able to use that to your advantage.

Brian

sjoshi wrote:
I'm able to use the Bacground Worker class to execute task and pain the
UI. However I want to be able to execute n tasks in order, say n1, then
n2 if there were no errors during n1, n3 so on.

I'm wondering what would be the best way to do this , do I wait in a
do...while loop for each to finish and then start another ?

thanks
Sunit
Dec 14 '06 #3

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

That's basically what you will have to do. It seems like you have three
tasks, but you want all three to run sequentially, just not on the UI
thread. You can still use the BackgroundWorker class for this, you just
have to do the first task with it, then the second, then the third, and so
on, and so on.

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

"sjoshi" <sj****@ingr.comwrote in message
news:11*********************@t46g2000cwa.googlegro ups.com...
I'm able to use the Bacground Worker class to execute task and pain the
UI. However I want to be able to execute n tasks in order, say n1, then
n2 if there were no errors during n1, n3 so on.

I'm wondering what would be the best way to do this , do I wait in a
do...while loop for each to finish and then start another ?

thanks
Sunit
Ok thanks I need to run a background task for each row ina dset. So I
tried this:

foreach (DAO.DAODatabase.DatabaseRow backupEntry in
_srvBackupDSet.Database)
{
InitAndStartBackgroundWorker(backupEntry);
while (!_lastRunFinished)
{
Thread.Sleep(500);
}
if (!_lastRunOk)
break;
}
private void InitAndStartBackgroundWorker(DAO.DAODatabase.Datab aseRow
backupRow)
{
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;

//Set background worker
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged += new
ProgressChangedEventHandler(worker_ProgressChanged );
worker.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(worker_RunWorkerCom pleted);

//Start Backup
worker.RunWorkerAsync(backupRow);
}

void worker_DoWork(object sender, DoWorkEventArgs e)
{
_lastRunFinished = false;
_backupMgr.PercentCompleted += new
PercentEventHandler(_backupMgr_PercentCompleted);
BackgroundWorker theWorker = sender as BackgroundWorker;
DoBackup((DAO.DAODatabase.DatabaseRow)e.Argument);
}

void worker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
_lastRunOk = false ;
UpdateGridForError(e.Error);
}
else
_lastRunOk = true;
_lastRunFinished = true;
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//code to UpdateGrid progress bar
}

However now the UI freezes and the 1st job itself never completes; i.e
the run worker_RunWorkerCompleted event is never raised.

It works fine if I just pass a single row.

thanks
Sunit

Dec 14 '06 #4
It looks like you are waiting on a loop until the processing is done.
You should be letting the events that the BackgroundThread fires tell you
when you are done instead of waiting for the variables to be set (which you
are doing the wrong way, since you are not locking access to them).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sjoshi" <sj****@ingr.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...
>
Nicholas Paldino [.NET/C# MVP] wrote:
>Sunit,

That's basically what you will have to do. It seems like you have
three
tasks, but you want all three to run sequentially, just not on the UI
thread. You can still use the BackgroundWorker class for this, you just
have to do the first task with it, then the second, then the third, and
so
on, and so on.

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

"sjoshi" <sj****@ingr.comwrote in message
news:11*********************@t46g2000cwa.googlegr oups.com...
I'm able to use the Bacground Worker class to execute task and pain the
UI. However I want to be able to execute n tasks in order, say n1, then
n2 if there were no errors during n1, n3 so on.

I'm wondering what would be the best way to do this , do I wait in a
do...while loop for each to finish and then start another ?

thanks
Sunit
Ok thanks I need to run a background task for each row ina dset. So I
tried this:

foreach (DAO.DAODatabase.DatabaseRow backupEntry in
_srvBackupDSet.Database)
{
InitAndStartBackgroundWorker(backupEntry);
while (!_lastRunFinished)
{
Thread.Sleep(500);
}
if (!_lastRunOk)
break;
}
private void InitAndStartBackgroundWorker(DAO.DAODatabase.Datab aseRow
backupRow)
{
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;

//Set background worker
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged += new
ProgressChangedEventHandler(worker_ProgressChanged );
worker.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(worker_RunWorkerCom pleted);

//Start Backup
worker.RunWorkerAsync(backupRow);
}

void worker_DoWork(object sender, DoWorkEventArgs e)
{
_lastRunFinished = false;
_backupMgr.PercentCompleted += new
PercentEventHandler(_backupMgr_PercentCompleted);
BackgroundWorker theWorker = sender as BackgroundWorker;
DoBackup((DAO.DAODatabase.DatabaseRow)e.Argument);
}

void worker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
_lastRunOk = false ;
UpdateGridForError(e.Error);
}
else
_lastRunOk = true;
_lastRunFinished = true;
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//code to UpdateGrid progress bar
}

However now the UI freezes and the 1st job itself never completes; i.e
the run worker_RunWorkerCompleted event is never raised.

It works fine if I just pass a single row.

thanks
Sunit

Dec 14 '06 #5

Nicholas Paldino [.NET/C# MVP] wrote:
It looks like you are waiting on a loop until the processing is done.
You should be letting the events that the BackgroundThread fires tell you
when you are done instead of waiting for the variables to be set (which you
are doing the wrong way, since you are not locking access to them).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sjoshi" <sj****@ingr.comwrote in message
news:11*********************@f1g2000cwa.googlegrou ps.com...

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

That's basically what you will have to do. It seems like you have
three
tasks, but you want all three to run sequentially, just not on the UI
thread. You can still use the BackgroundWorker class for this, you just
have to do the first task with it, then the second, then the third, and
so
on, and so on.

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

"sjoshi" <sj****@ingr.comwrote in message
news:11*********************@t46g2000cwa.googlegro ups.com...
I'm able to use the Bacground Worker class to execute task and pain the
UI. However I want to be able to execute n tasks in order, say n1, then
n2 if there were no errors during n1, n3 so on.

I'm wondering what would be the best way to do this , do I wait in a
do...while loop for each to finish and then start another ?

thanks
Sunit
Ok thanks I need to run a background task for each row ina dset. So I
tried this:

foreach (DAO.DAODatabase.DatabaseRow backupEntry in
_srvBackupDSet.Database)
{
InitAndStartBackgroundWorker(backupEntry);
while (!_lastRunFinished)
{
Thread.Sleep(500);
}
if (!_lastRunOk)
break;
}
private void InitAndStartBackgroundWorker(DAO.DAODatabase.Datab aseRow
backupRow)
{
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;

//Set background worker
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged += new
ProgressChangedEventHandler(worker_ProgressChanged );
worker.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(worker_RunWorkerCom pleted);

//Start Backup
worker.RunWorkerAsync(backupRow);
}

void worker_DoWork(object sender, DoWorkEventArgs e)
{
_lastRunFinished = false;
_backupMgr.PercentCompleted += new
PercentEventHandler(_backupMgr_PercentCompleted);
BackgroundWorker theWorker = sender as BackgroundWorker;
DoBackup((DAO.DAODatabase.DatabaseRow)e.Argument);
}

void worker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
_lastRunOk = false ;
UpdateGridForError(e.Error);
}
else
_lastRunOk = true;
_lastRunFinished = true;
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//code to UpdateGrid progress bar
}

However now the UI freezes and the 1st job itself never completes; i.e
the run worker_RunWorkerCompleted event is never raised.

It works fine if I just pass a single row.

thanks
Sunit
Yes I forgot about the locking...will that be the reason for freeeze ?
I do set the variables in the RunWorkerCompleted event so that I can
know when to start another one and as such I can create a new
BackgroundWorker in the InitAndStart method. Is this the correct way to
start next task ?
Can you please let me know what I'm doing wrong here (pseudo code...)

thanks
Sunit

Dec 14 '06 #6
"sjoshi" <sj****@ingr.comwrote in message
news:11*********************@t46g2000cwa.googlegro ups.com...
I'm able to use the Bacground Worker class to execute task and pain the
UI. However I want to be able to execute n tasks in order, say n1, then
n2 if there were no errors during n1, n3 so on.

I'm wondering what would be the best way to do this , do I wait in a
do...while loop for each to finish and then start another ?
If you want tasks processed sequentially, you need a queue of some sort.

One way to do this would be to take advantage of the suggestions offered so
far. You would add the tasks to the queue, and then use the event Brian
mentions to know when to start a new task thread.

Alternatively, you could just keep a single background thread running that
pulls tasks from the queue as long as the queue isn't empty. Once the queue
is empty, the background thread could wait on an EventWaitHandle that the
main thread would set any time it adds something to the queue.

In either approach, you can include logic to check the results of the
previous task before starting the next one and just abort the whole thing if
an error occurs.

For sure, the thread synchronization you've tried in the code you posted
already is wrong. It appears that you're polling (which is why the main UI
freezes up...you never leave the loop until all tasks are done), and you're
checking an unsynchronized flag (which isn't thread-safe).

Pete
Dec 14 '06 #7

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

Similar topics

33
by: bonk | last post by:
I have an application that needs to perform some background work, i.e. Logging, wich must not block the main thread. How would I basically design such a scenario? It is obvious that I should do...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
3
by: Kevin | last post by:
Using this: http://msdn2.microsoft.com/en-us/library/3dasc8as(VS.80).aspx as an example I have a question concerning the reuse of objects. In the example 10 instances of the Fibonacci class...
13
by: deciacco | last post by:
How can I have access to the items collection of a listview control on my form from a background thread? I know I need delegates to update the listview control and I have those calls in the...
4
by: csharpula csharp | last post by:
Hello, Can you please tell me if I should use the background worker in the following way: if (worker.IsBusy) worker.CancelAsync(); (or I can avoid this if)? And in case I want to restart the...
0
by: Smokey Grindel | last post by:
I have a search function that runs in a background worker which can run long.. I want the user to be able to cancel that then run another search... but sometimes it throws an exception saying ...
8
by: Analizer1 | last post by:
From what im reading about backgroundworker thread it seems it can only have 1 thread at a time is this correct tks
7
by: csharpula csharp | last post by:
Hello, I got a question regarding the usage of background worker. How can I run few threads via background worker with different objects as parameter each time. I understood that I can't do...
1
by: Infog | last post by:
I am trying to code a simple background workers class without needing to use the backgroundworker object on a form. This would allow me to use a background worker in any class without writing much...
0
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...
1
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...
0
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...
1
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...
0
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.