问题
I'm trying to get this BackgroundWorker to work. When you click the View Orders button, it will display a message along the lines of "Retrieving new orders..." etc, and will do background work (mysql query), now, there's a bunch of stuff inside the DoWork method, and none of it gets done.
I know it's not because of the MySQL Query because it works fine on its own without the background worker.
Here's the code:
private void ViewOrders_Click(object sender, EventArgs e)
{
SlideTimer.Enabled = true;
Alert("Retrieving unconfirmed orders. Please wait.",
"Cancel",
Information,
true,
Color.FromArgb(63, 187, 249)
);
action = Action.ViewOrder;
bWorker.WorkerSupportsCancellation = true;
bWorker.DoWork += new DoWorkEventHandler(bWorker_DoWork);
bWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bWorker_RunWorkerCompleted);
bWorker.RunWorkerAsync();
}
void bWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
SlideTimer.Enabled = true;
Alert("All unconfirmed orders have been retrieved.",
"Dismiss",
Information,
true,
Color.FromArgb(63, 187, 249)
);
action = Action.None;
}
void bWorker_DoWork(object sender, DoWorkEventArgs e)
{
// Connect to Database, check for orders, and end.
if (bWorker.CancellationPending == true)
{
e.Cancel = true;
}
else
{
if (action == Action.ViewOrder)
{
thelist.Clear();
thelist.Visible = true;
thelist.BringToFront();
MessageBox.Show("");
thelist.Columns.Add("Order #");
thelist.Columns.Add("Name");
thelist.Columns.Add("E-mail Address");
thelist.Columns.Add("Delivery Address");
thelist.Columns.Add("Company");
thelist.Columns.Add("Phone Number");
// Check for new orders.
MySql.Data.MySqlClient.MySqlConnection msc = new MySql.Data.MySqlClient.MySqlConnection(cs);
try
{
msc.Open();
// Check for orders now.
string st = "SELECT DISTINCT(sessionid), firstname, lastname, email, streetaddress, suburb, postcode, state, company, phone FROM mysql_9269_dbase.order";
MySql.Data.MySqlClient.MySqlCommand cd = new MySql.Data.MySqlClient.MySqlCommand(st, msc);
MySql.Data.MySqlClient.MySqlDataReader msdr = cd.ExecuteReader();
while (msdr.Read())
{
if (thelist.Items.Count == 0)
{
ListViewItem LItem = new ListViewItem(msdr[0].ToString());
ListViewItem.ListViewSubItemCollection SubItems = new ListViewItem.ListViewSubItemCollection(LItem);
SubItems.Add(msdr[1].ToString() + " " + msdr[2].ToString());
SubItems.Add(msdr[3].ToString());
SubItems.Add(msdr[4].ToString() + " " + msdr[5].ToString() + " " + msdr[6].ToString() + " " + msdr[7]);
SubItems.Add(msdr[8].ToString());
SubItems.Add(msdr[9].ToString());
thelist.Items.Add(LItem);
thelist.Update();
}
else
{
sound.Play();
//status.Text = "Records found; Retrieving now.";
var found = false;
foreach (var item in thelist.Items)
{
if (item.ToString().Contains(msdr[0].ToString()))
found = true;
}
if (thelist.Items.Count == 0 || !found)
{
ListViewItem LItem = new ListViewItem(msdr[0].ToString());
ListViewItem.ListViewSubItemCollection SubItems = new ListViewItem.ListViewSubItemCollection(LItem);
SubItems.Add(msdr[1].ToString() + " " + msdr[2].ToString());
SubItems.Add(msdr[3].ToString());
SubItems.Add(msdr[4].ToString() + " " + msdr[5].ToString() + " " + msdr[6].ToString() + " " + msdr[7]);
SubItems.Add(msdr[8].ToString());
SubItems.Add(msdr[9].ToString());
thelist.Items.Add(LItem);
thelist.Update();
}
}
}
}
catch (Exception en)
{
Alert(en.Message,
"Retry",
Error,
true,
Color.FromArgb(249, 87, 55)
);
}
msc.Close();
}
thelist.Visible = true;
thelist.BringToFront();
}
}
private void MessageLink_Click(object sender, EventArgs e)
{
switch (MessageLink.Text)
{
case "Cancel":
bWorker.CancelAsync();
SlideTimer.Enabled = true;
Alert("You have successfully cancelled the current operation.",
"Dismiss",
Information,
true,
Color.FromArgb(63, 187, 249)
);
action = Action.None;
break;
case "":
break;
default:
break;
}
}
There's no errors or anything. Just that Nothing happens. What's causing the Background(so called)Worker to not DoWork()?
*Sorry for the lengthy code snip.
回答1:
You've hooked up the callbacks but not actually called RunWorkerAsync to start it going.
As an aside, you're also calling UI elements in the DoWork method which will fail. You need to use BeginInvoke to mashal the updates back to the UI thread or do the updates in the RunWorkerComplete method (which is run on the UI thread automatically).
回答2:
MessageBox.Show("");
This will cause a big problem.
You can't call UI from a non UI thread. I'm surprised that it hasn't raised an exception. At best it's probably causing your thread to abort and not do anything.
The theList
looks like a UI element as well. If you are adding to control then you need to pass the data via an event to the UI so you can update it there.
You also have:
Alert(en.Message,
"Retry",
Error,
true,
Color.FromArgb(249, 87, 55)
);
In your exception handler. Again this looks like you are trying to call UI elements.
来源:https://stackoverflow.com/questions/5364259/c-sharp-backgroundworker-not-working