Bulldog.NET

Hi,

I have a background worker in the window form app that retrieves database records and report back the progress. The problem is if it runs it hangs up the form and I cannot click the cmdCancelWorker button since the form is not responding. This is the code I have written below:-

namespace BackGroundWorkerTest

{

public partial class Form1 : Form

{

BackgroundWorker worker = new BackgroundWorker();

public Form1()

{

InitializeComponent();

InitializeBackGroundWorker();

}

private void InitializeBackGroundWorker()

{

worker.DoWork += new DoWorkEventHandler(worker_DoWork);

worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);

worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

worker.WorkerSupportsCancellation = true;

worker.WorkerReportsProgress = true;

}

private void cmdStartWorker_Click(object sender, EventArgs e)

{

cmdStartWorker.Enabled = false;

worker.RunWorkerAsync();

}

private void worker_DoWork(object sender, DoWorkEventArgs e)

{

try

{

int NumberOfRecords = 0;

int progress = 0;

int currentIndex = 0;

List<DateTime> ListDateTime = new List<DateTime>();

string sqlCmdGetTotal = "SELECT COUNT(*) FROM dimTime";

string sqlCmdGetTableDates = "SELECT date FROM dimTime";

using (SqlConnection TimeDBConnection = new SqlConnection(ConfigurationSettings.AppSettings["TimeDB"]))

{

TimeDBConnection.Open();

using (SqlCommand SqlcmdCountRecords = new SqlCommand(sqlCmdGetTotal, TimeDBConnection))

{

NumberOfRecords = (int)SqlcmdCountRecords.ExecuteScalar();

}

using (SqlCommand SQLcmdGetDates = new SqlCommand(sqlCmdGetTableDates, TimeDBConnection))

{

using (SqlDataReader sqlReadDates = SQLcmdGetDates.ExecuteReader())

{

while (sqlReadDates.Read())

{

ListDateTime.Add((DateTime)sqlReadDates[0]);

currentIndex++;

progress = (int)((currentIndex * 100) / NumberOfRecords);

worker.ReportProgress(progress);

if (worker.CancellationPending)

{

e.Cancel = true;

return;

}

}

}

}

TimeDBConnection.Close();

}

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)

{

workerProgressBar.Value = e.ProgressPercentage;

workerStatusLabel.Text = string.Format("{0} %", e.ProgressPercentage.ToString());

}

private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

if (e.Cancelled)

workerStatusLabel.Text = "Task cancelled";

else

workerStatusLabel.Text = "Task complete";

cmdStartWorker.Enabled = true;

}

private void cmdCancelWorker_Click(object sender, EventArgs e)

{

if (worker.IsBusy)

worker.CancelAsync();

else

workerStatusLabel.Text = "Task is not running";

}

}

}


Does anybody knows why the form just hang-up




Re: Windows Forms General Why the background worker hangup the Form in Win App?

Yu Guo ¨C MSFT

Hi, Bulldog.NET,

I cannot reproduce this issue.

Could you please debug it to see what has caused the hangup

Regards






Re: Windows Forms General Why the background worker hangup the Form in Win App?

Yu Guo ¨C MSFT

We are changing the issue type to ˇ°Commentˇ± because you have not followed up with the necessary information. If you have more time to look at the issue and provide more information, please feel free to change the issue type back to ˇ°Questionˇ± by editing your initial post and changing the radio button at the top of the post editor window. If the issue is resolved, we will appreciate it if you can share the solution so that the answer can be found and used by other community members having similar questions.
Thank you!






Re: Windows Forms General Why the background worker hangup the Form in Win App?

Bulldog.NET

Hi Yu Guo

Sorry for the late response, I decided to use a delegate to report the progress and it works fine here is the code below:

namespace BackGroundWorkerTest

{

public delegate void UpdateProgess(int progress);

public partial class Form1 : Form

{

BackgroundWorker worker = new BackgroundWorker();

public Form1()

{

InitializeComponent();

InitializeBackGroundWorker();

}

private void InitializeBackGroundWorker()

{

worker.DoWork += new DoWorkEventHandler(worker_DoWork);

worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

worker.WorkerSupportsCancellation = true;

}

private void cmdStartWorker_Click(object sender, EventArgs e)

{

cmdStartWorker.Enabled = false;

worker.RunWorkerAsync();

}

private void worker_DoWork(object sender, DoWorkEventArgs e)

{

try

{

int NumberOfRecords = 0;

int progress = 0;

int currentIndex = 0;

List<DateTime> ListDateTime = new List<DateTime>();

string sqlCmdGetTotal = "SELECT COUNT(*) FROM dimTime";

string sqlCmdGetTableDates = "SELECT date FROM dimTime";

using (SqlConnection TimeDBConnection = new SqlConnection(ConfigurationSettings.AppSettings["TimeDB"]))

{

TimeDBConnection.Open();

using (SqlCommand SqlcmdCountRecords = new SqlCommand(sqlCmdGetTotal, TimeDBConnection))

{

NumberOfRecords = (int)SqlcmdCountRecords.ExecuteScalar();

}

using (SqlCommand SQLcmdGetDates = new SqlCommand(sqlCmdGetTableDates, TimeDBConnection))

{

using (SqlDataReader sqlReadDates = SQLcmdGetDates.ExecuteReader())

{

while (sqlReadDates.Read())

{

ListDateTime.Add((DateTime)sqlReadDates[0]);

currentIndex++;

progress = (int)((currentIndex * 100) / NumberOfRecords);

Invoke(new UpdateProgess(ProgressChanged), (progress)); //Report Progress

if (worker.CancellationPending)

{

e.Cancel = true;

return;

}

}

}

}

TimeDBConnection.Close();

}

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString(), "Application Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

private void ProgressChanged(int progress)

{

workerProgressBar.Value = progress;

workerStatusLabel.Text = string.Format("{0} %", progress.ToString());

}

private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

{

if (e.Cancelled)

workerStatusLabel.Text = "Task cancelled";

else

workerStatusLabel.Text = "Task complete";

cmdStartWorker.Enabled = true;

}

private void cmdCancelWorker_Click(object sender, EventArgs e)

{

if (worker.IsBusy)

worker.CancelAsync();

else

workerStatusLabel.Text = "Task is not running";

}

}

}


Kind Regards,