C# Windows 表单 - 每次我 运行 我的代码时,消息框都会显示一次
C# Windows Forms - Messagebox showing once more every time I run my code
我有一个测试排序的 Windows 表单应用程序 algorithms.It 由单个 form.When 测试组成,完成该表单后会显示一个消息框,表明测试 done.But第一次测试显示一次,第二次显示两次,第三次显示三次,依此类推。
显示 MessageBox 的方法由 "RunWorkerCompleted" 方法的后台工作程序调用。
private void RunAlgorithmTests()
//this is called from an onclick method
{
backgroundWorker.DoWork += (s, e) =>
{
test.RunTests();
};
backgroundWorker.RunWorkerCompleted += (s, e) =>
{
ShowCompleteMessage();
};
backgroundWorker.RunWorkerAsync();
}
private void ShowCompleteMessage()
//show this message on successful test complete
{
string title = "Test završen!";
string message = "Test završen nakon" + swatch.Elapsed.Hours.ToString() + ":" + swatch.Elapsed.Minutes.ToString() + ":" + swatch.Elapsed.Seconds.ToString();
this.Text = "Aplikacija za testiranje algoritama sortiranja";
if (test.complete)
{
MessageBox.Show(message, title);
}
}
MessageBox 应该只在每次测试结束时出现一次,但它出现的次数会随着每次测试的增加而增加 运行。
按照建议,只连接这些事件一次。这是从 Form 的 Load() 事件完成的:
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker.DoWork += (s, e) =>
{
test.RunTests();
};
backgroundWorker.RunWorkerCompleted += (s, e) =>
{
ShowCompleteMessage();
};
}
private void button1_Click(object sender, EventArgs e)
{
RunAlgorithmTests();
}
//this is called from an onclick method
private void RunAlgorithmTests()
{
backgroundWorker.RunWorkerAsync();
}
问题在于,每次您 运行 后台工作程序时,您都会为这两个事件添加额外的新处理程序。第一次,每个人都有一个处理程序。下次,您再添加一个,这样您就有了两个。两者都被调用。下一次,您添加另一个,所有三个都被跟注。
您要做的是确保只发生一次。
我喜欢你将事件处理程序初始化放在它自己的方法中的想法,所以我们会坚持下去。
// Your Form class is probably called something else.
public Form1()
{
InitializeComponent();
// Since backgroundWorker was created in the form designer, it will have been
// initialized in InitializeComponent(). Therefore, this has to happen after
// InitializeComponent() is called.
InitializeBackgroundWorkerHandlers();
}
private void InitializeBackgroundWorkerHandlers()
{
backgroundWorker.DoWork += (s, e) =>
{
test.RunTests();
};
backgroundWorker.RunWorkerCompleted += (s, e) =>
{
ShowCompleteMessage();
};
}
// this is called from an onclick method
private void RunAlgorithmTests()
{
backgroundWorker.RunWorkerAsync();
}
我有一个测试排序的 Windows 表单应用程序 algorithms.It 由单个 form.When 测试组成,完成该表单后会显示一个消息框,表明测试 done.But第一次测试显示一次,第二次显示两次,第三次显示三次,依此类推。 显示 MessageBox 的方法由 "RunWorkerCompleted" 方法的后台工作程序调用。
private void RunAlgorithmTests()
//this is called from an onclick method
{
backgroundWorker.DoWork += (s, e) =>
{
test.RunTests();
};
backgroundWorker.RunWorkerCompleted += (s, e) =>
{
ShowCompleteMessage();
};
backgroundWorker.RunWorkerAsync();
}
private void ShowCompleteMessage()
//show this message on successful test complete
{
string title = "Test završen!";
string message = "Test završen nakon" + swatch.Elapsed.Hours.ToString() + ":" + swatch.Elapsed.Minutes.ToString() + ":" + swatch.Elapsed.Seconds.ToString();
this.Text = "Aplikacija za testiranje algoritama sortiranja";
if (test.complete)
{
MessageBox.Show(message, title);
}
}
MessageBox 应该只在每次测试结束时出现一次,但它出现的次数会随着每次测试的增加而增加 运行。
按照建议,只连接这些事件一次。这是从 Form 的 Load() 事件完成的:
private void Form1_Load(object sender, EventArgs e)
{
backgroundWorker.DoWork += (s, e) =>
{
test.RunTests();
};
backgroundWorker.RunWorkerCompleted += (s, e) =>
{
ShowCompleteMessage();
};
}
private void button1_Click(object sender, EventArgs e)
{
RunAlgorithmTests();
}
//this is called from an onclick method
private void RunAlgorithmTests()
{
backgroundWorker.RunWorkerAsync();
}
问题在于,每次您 运行 后台工作程序时,您都会为这两个事件添加额外的新处理程序。第一次,每个人都有一个处理程序。下次,您再添加一个,这样您就有了两个。两者都被调用。下一次,您添加另一个,所有三个都被跟注。
您要做的是确保只发生一次。
我喜欢你将事件处理程序初始化放在它自己的方法中的想法,所以我们会坚持下去。
// Your Form class is probably called something else.
public Form1()
{
InitializeComponent();
// Since backgroundWorker was created in the form designer, it will have been
// initialized in InitializeComponent(). Therefore, this has to happen after
// InitializeComponent() is called.
InitializeBackgroundWorkerHandlers();
}
private void InitializeBackgroundWorkerHandlers()
{
backgroundWorker.DoWork += (s, e) =>
{
test.RunTests();
};
backgroundWorker.RunWorkerCompleted += (s, e) =>
{
ShowCompleteMessage();
};
}
// this is called from an onclick method
private void RunAlgorithmTests()
{
backgroundWorker.RunWorkerAsync();
}