当前位置: 代码迷 >> C# >> 进度条过程应该怎样结束
  详细解决方案

进度条过程应该怎样结束

热度:393   发布时间:2016-05-05 04:40:11.0
进度条进程应该怎样结束
 public partial class frm_sync3 : Form
    {
        public frm_sync3()
        {
            InitializeComponent();

            ThreadPool.QueueUserWorkItem((object o) =>
            {
                for (int i = 1; i <= 100; i++)
                {
                    System.Threading.Thread.Sleep(100);
                    this.label1.Invoke(new Action(() => this.label1.Text = i + "%"));
                    this.progressBar1.Invoke(new Action(() => this.progressBar1.Value = i));

                }
            });
        }

       
        private void button2_Click(object sender, EventArgs e)
        {
            //结束进度条进程
            this.DialogResult = DialogResult.Cancel;
        }
    }

请问绿色字的内容我应该怎么实现,因为领导说进程不结束就关闭页面是不对的,请问我应该怎样实现
------解决思路----------------------
那你就这样写,直接暴力结束线程

        Thread thread;
        private void button1_Click(object sender, EventArgs e)
        {
            if (thread == null)
            {
                thread = new Thread(() =>
                    {
                        try
                        {
                            for (int i = 1; i <= 100; i++)
                            {
                                this.label1.Invoke(new Action(() => this.label1.Text = i + "%"));
                                this.progressBar1.Invoke(new Action(() => this.progressBar1.Value = i));
                                Thread.Sleep(100);
                            }
                        }
                        catch { }
                    });
                thread.IsBackground = true;
                thread.Start();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (thread != null)
            {
                try
                {
                    thread.Abort();
                }
                catch { }
            }
        }
  相关解决方案