我用C#构建了我的第一个Windows窗体,我开始遇到一些没有响应的问题。这只发生在我开始点击其他地方或开始做其他事情时。我研究了一些并了解到线程可能有所帮助,但无法弄清楚我应该如何做到这一点。以下是我现在正在尝试的内容。
private void bPrint_Click(object sender, EventArgs e)
{
bPrint.Enabled = false;
string cusNo = Strings.Left(cbCustomerList.SelectedItem.ToString(), 4);
ReportDocument reportDocument = new ReportDocument();
switch (cusNo)
{
case "4018":
try
{
Thread backgroundThread2 = new Thread(
new ThreadStart(() =>
FixPhoneNumbers(orderData, cusNo, dtpBeginDate.Value, dtpEndDate.Value,
Settings.Default["Location"].ToString(), lProgress)));
backgroundThread2.Start();
Thread backgroundThread3 = new Thread(
new ThreadStart(() =>
CreateBol(orderData, cusNo, dtpBeginDate.Value, dtpEndDate.Value,
Settings.Default["Location"].ToString(), lProgress)));
backgroundThread3.Start();
Thread backgroundThread4 = new Thread(
new ThreadStart(() =>
CreateUccPerLine(orderData, cusNo, dtpEndDate.Value, dtpEndDate.Value,
Settings.Default["Location"].ToString(), lProgress)));
backgroundThread4.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Loading " + ex.Message, MessageBoxButtons.OK,
MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1,
MessageBoxOptions.ServiceNotification);
}
finally
{
Thread backgroundThread1 = new Thread(
new ThreadStart(() =>
AssignProNumber(orderData, cusNo, dtpBeginDate.Value, dtpEndDate.Value,
Settings.Default["Location"].ToString(), lProgress)));
backgroundThread1.Start();
LoadDS(orderData, cusNo, dtpBeginDate.Value, dtpEndDate.Value,
Settings.Default["Location"].ToString(), lProgress);
PrintReport(orderData, reportDocument, "psWalmart.rpt",
Settings.Default["RegularPrinter"].ToString(), Settings.Default["LabelPrinter"].ToString());
PrintReport(orderData, reportDocument, "lbWalmart.rpt",
Settings.Default["RegularPrinter"].ToString(), Settings.Default["LabelPrinter"].ToString());
PrintReport(orderData, reportDocument, "boWalmart.rpt",
Settings.Default["RegularPrinter"].ToString(), Settings.Default["LabelPrinter"].ToString());
PrintReport(orderData, reportDocument, "qtyOrderedReport.rpt",
Settings.Default["RegularPrinter"].ToString(), Settings.Default["LabelPrinter"].ToString());
PrintReport(orderData, reportDocument, "qtyOrderedGreaterOne.rpt",
Settings.Default["RegularPrinter"].ToString(), Settings.Default["LabelPrinter"].ToString());
}
break;
}
答案 0 :(得分:3)
这样做的新方法是使用Task
示例强>
private void bPrint_Click(object sender, EventArgs e)
{
await Task.Run(() =>
{
FixPhoneNumbers(orderData, cusNo, dtpBeginDate.Value, dtpEndDate.Value,
Settings.Default["Location"].ToString(), lProgress)))
});
}
您还可以运行多项任务
private void bPrint_Click(object sender, EventArgs e)
{
var task1 = DoWorkAsync();
var task2 = DoMoreWorkAsync();
await Task.WhenAll(task1, task2);
}
答案 1 :(得分:0)
在线程内执行的代码按顺序执行,因此要一个接一个地运行多个方法,只需将它们写在线程代码中的不同行中,就像使用任何C#代码一样。
我强烈建议在c#而不是线程中查看Tasks,它们会更容易一些。
同样可能Async / Await值得一试,但我不确定winforms将如何支持它。
答案 2 :(得分:-1)
瑞恩
我使用线程基本上处理ASP.NET站点中的批处理。该线程进程将在另一个线程中运行ProcessMyData()方法。
//Create new object and start processing
MySite.Data.ProcessData Batch = new Data.ProcessData();
Batch.StartBatchProcessing();
//The StartBatchProcessing() Creates the new thread
public void StartBatchProcessing()
{
//ProcessMyData is the main method that will process the batch data
Thread newThread = new Thread(this.ProcessMyData); //another thread
newThread.Priority = ThreadPriority.Normal;
newThread.Start();
}