【问题标题】:how to stop the program when press cancel button at the progress bar c#在进度条上按取消按钮时如何停止程序c#
【发布时间】:2011-09-01 13:06:57
【问题描述】:

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

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

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);

【问题讨论】:

    标签: c# winforms encryption progress-bar cancel-button


    【解决方案1】:

    您应该为此使用BackgroundWorker,而不是System.Threading.Thread

    【讨论】:

    • +1 同意。后台工作人员内置了取消条款。
    【解决方案2】:

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 2021-02-15
      相关资源
      最近更新 更多