gpt4 book ai didi

C# BackgroundWorker 不工作

转载 作者:太空宇宙 更新时间:2023-11-03 10:26:11 25 4
gpt4 key购买 nike

我正试图让这个 BackgroundWorker 工作。当你点击 View Orders 按钮时,它会显示一条消息,如“正在检索新订单...”等,并且会做后台工作(mysql 查询),现在,DoWork 方法中有一堆东西,并且一切都没有完成。

我知道这不是因为 MySQL 查询,因为它可以在没有后台工作人员的情况下自行正常工作。

代码如下:

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;
}
}

没有任何错误。只是什么都没有发生。是什么导致后台(所谓的)Worker 不执行 DoWork()?

*对于冗长的代码片段,我们深表歉意。

最佳答案

您已经连接了回调,但实际上并没有调用 RunWorkerAsync 来启动它。

顺便说一句,您还在 DoWork 方法中调用 UI 元素,这将失败。您需要使用 BeginInvoke 将更新混搭回 UI 线程或在 RunWorkerComplete 方法(自动在 UI 线程上运行)中执行更新。

关于C# BackgroundWorker 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5364259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com