如何在按进度条上的取消按钮时停止程序c#

发布于 2024-12-02 14:50:07 字数 1269 浏览 2 评论 0原文

我正在尝试制作一个进度条表单加载,在加载时,如果我按下取消按钮,整个应用程序将停止它正在执行的任何操作。

下面是我调用进度条表单的代码。我想知道有人可以帮忙解决这个问题吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

噩梦成真你也成魔 2024-12-09 14:50:08

为此,您应该使用 BackgroundWorker,而不是 <代码>System.Threading.Thread。

You should use a BackgroundWorker for this, instead of System.Threading.Thread.

淑女气质 2024-12-09 14:50:08

Application.Restart() 应该可以很好地阻止它;)

不过,严肃地说,MSDN 网站上有大量关于此类内容的信息。我会用它来至少了解要搜索的内容。并不是说您还不知道,但它提供了有关确保您使用正确术语(例如停止与取消)以及其他类似内容的大量信息。

http://msdn.microsoft.com/en-us/library/aa511486.aspx

如果你检查
http://msdn.microsoft.com/en-us/library/ywkkz4s1.aspx 然后你会发现一个很棒的资源,关于如何启动后台工作程序(应该用于此目的)以及如何实现取消按钮。窃取该代码并将其插入您的程序中。希望这有帮助!

编辑:环顾四周后,我认为您可能想要调用的代码在 MSDN 站点上看起来有点像这样:

 private void Cancel_Click(object sender, EventArgs e)
{
    // Cancel the asynchronous operation.
    this.backgroundWorker1.CancelAsync();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文