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]
- (e-mail address removed)
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.DatabaseRow
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_RunWorkerCompleted);
//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