Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Environment: Visual Studio 2010 (C#), SQL Server 2012, DB2
Type of Project: Windows Forms

Problem description:
I have a time-consuming process for getting data from SQL Server db:

private void getAllPartsByDrawingNo(string sValue) with no returning value
or it could be:
private int getAllPartsByDrawingNoFunc(string sValue) which return 1.

I would like to use BackgroundWorker and ProgressBar to show the progress (see code below).

Question: I understand that I have to call above function somehow in RunWorkerAsync(???) overload but I cannot figure out how to do that, or maybe I completely wrong in that? If possible show me a good example

Code:
private void btnStart2_Click(object sender, EventArgs e)
{
btnStart2.Enabled = false;
btnCancel2.Enabled = true;

backgroundWorker1.RunWorkerAsync(???);
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(100);

if (i % 5 == 0)
{
backgroundWorker1.ReportProgress(i);
}

if(backgroundWorker1.CancellationPending)
{
e.Cancel = true;
backgroundWorker1.ReportProgress(0);
return;
}
}
backgroundWorker1.ReportProgress(100);
}
…and so far..
Posted

1 solution

No - have a look at the MSDN documentation, it includes an example.

You call the actual function in the BackgroundWorker.DoWork event handler, and report progress there, via the sender parameter which is the worker instance.
Your main thread then handles the ProgressChanged event and displays the updated value to the user.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900