概要:
我正在尝试将非线程应用程序转换为线程应用程序。我已经有 10 年没有在应用程序中使用线程了,我对这个概念非常生疏。只是有些东西我没有掌握。您能否在代码之前和之后查看我的(非常短的)以了解我做错了什么以及为什么错了?非常感谢所有帮助!谢谢!
看到的错误:
我的代码没有响应。我可以运行并执行按钮来启动应用程序,但不是启动一个新线程并正常工作,它们会冻结并显示“无响应”消息。我不确定我做错了什么。我只是错误地实现了线程吗?
非线程代码:
//this opens the user input windows form
private void UserInputButton_Click(object sender, EventArgs e)
{
enumerationStation EnumerationForm = new enumerationStation();
EnumerationForm.Show();
//userform UserForm = new userform();
//UserForm.Show();
}
//this opens the correlationApplication windows form
private void CorrelationApplication_Click(object sender, EventArgs e)
{
CorrelationApplication CorrelationApplicationForm = new CorrelationApplication();
CorrelationApplicationForm.Show();
}
损坏的线程代码:
//this opens the user input windows form
private void UserInputButton_Click(object sender, EventArgs e)
{
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += new DoWorkEventHandler(enumerationStuff);
bg.RunWorkerAsync();
}
//run enumeration application in new thread
private void enumerationStuff(object sender, DoWorkEventArgs e)
{
enumerationStation EnumerationForm = new enumerationStation();
EnumerationForm.Show();
}
//this opens the correlationApplication windows form
private void CorrelationApplication_Click(object sender, EventArgs e)
{
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += new DoWorkEventHandler(correlationStuff);
bg.RunWorkerAsync();
}
//run correlation application in new thread
private void correlationStuff(object sender, DoWorkEventArgs e)
{
CorrelationApplication CorrelationApplicationForm = new CorrelationApplication();
CorrelationApplicationForm.Show();
}