how to stop the program when press cancel button at the progress bar c#
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();
d开发者_StackOverflow社区etails.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);
You should use a BackgroundWorker for this, instead of System.Threading.Thread
.
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:
private void Cancel_Click(object sender, EventArgs e)
{
// Cancel the asynchronous operation.
this.backgroundWorker1.CancelAsync();
}
精彩评论