如何在按进度条上的取消按钮时停止程序c#
我正在尝试制作一个进度条表单加载,在加载时,如果我按下取消按钮,整个应用程序将停止它正在执行的任何操作。
下面是我调用进度条表单的代码。我想知道有人可以帮忙解决这个问题吗?
Loading loader = new Loading();
private void lockButton_Click(object sender, EventArgs e)
{
if (this.ddCheckBox.Checked == false)
{
if (this.passwordtextBox.Text == "")
{
MessageBox.Show("Please enter a password!");
}
else if (this.retypeTextBox.Text == "")
{
MessageBox.Show("Please retype password!");
}
else if (this.passwordtextBox.Text == this.retypeTextBox.Text)
{
//details = new Details();
details.SetPassword(this.passwordtextBox.Text);
if (this.EncryptionComboBox.Text == "AES - 128 bit" | this.EncryptionComboBox.Text == "AES - 192 bit" | this.EncryptionComboBox.Text == "AES - 256 bit")
{
this.Hide();
Thread thread = null;
thread = new Thread(new ThreadStart(delegate() { loader.dLabel.Text = "Locking Files..."; loader.ShowDialog(); }));
thread.Start();
details.SetEncryption(this.EncryptionComboBox.Text);
I am trying to make a progress bar form load and while loading, if I press the cancel button the whole application will stop whatever it is doing.
Below is my code calling the progress bar form. I was wondering anyone could help out with this?
Loading loader = new Loading();
private void lockButton_Click(object sender, EventArgs e)
{
if (this.ddCheckBox.Checked == false)
{
if (this.passwordtextBox.Text == "")
{
MessageBox.Show("Please enter a password!");
}
else if (this.retypeTextBox.Text == "")
{
MessageBox.Show("Please retype password!");
}
else if (this.passwordtextBox.Text == this.retypeTextBox.Text)
{
//details = new Details();
details.SetPassword(this.passwordtextBox.Text);
if (this.EncryptionComboBox.Text == "AES - 128 bit" | this.EncryptionComboBox.Text == "AES - 192 bit" | this.EncryptionComboBox.Text == "AES - 256 bit")
{
this.Hide();
Thread thread = null;
thread = new Thread(new ThreadStart(delegate() { loader.dLabel.Text = "Locking Files..."; loader.ShowDialog(); }));
thread.Start();
details.SetEncryption(this.EncryptionComboBox.Text);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您应该使用 BackgroundWorker,而不是 <代码>System.Threading.Thread。
You should use a BackgroundWorker for this, instead of
System.Threading.Thread
.Application.Restart() 应该可以很好地阻止它;)
不过,严肃地说,MSDN 网站上有大量关于此类内容的信息。我会用它来至少了解要搜索的内容。并不是说您还不知道,但它提供了有关确保您使用正确术语(例如停止与取消)以及其他类似内容的大量信息。
http://msdn.microsoft.com/en-us/library/aa511486.aspx
如果你检查
http://msdn.microsoft.com/en-us/library/ywkkz4s1.aspx 然后你会发现一个很棒的资源,关于如何启动后台工作程序(应该用于此目的)以及如何实现取消按钮。窃取该代码并将其插入您的程序中。希望这有帮助!
编辑:环顾四周后,我认为您可能想要调用的代码在 MSDN 站点上看起来有点像这样:
Application.Restart() should stop it pretty well ;)
In all seriousness, though, there's a TON of information on stuff like this on the MSDN website. I'd use this to at least get an idea of what to search for. Not that you don't know already, but it has great information on making sure you're using the correct terms (like stop vs. cancel), and some other stuff like that.
http://msdn.microsoft.com/en-us/library/aa511486.aspx
If you check
http://msdn.microsoft.com/en-us/library/ywkkz4s1.aspx then you'll find an awesome resource on both how to start a background worker (which should be used for this) and how to implement a cancel button. Steal that code and plug it into your program. Hope this helps!
EDIT: After looking around a bit, I think the code you might want to call would look a bit like this from the MSDN site: