zoukankan      html  css  js  c++  java
  • C# BackgroundWorker的Bug???

    废话不多说,上代码:

    public partial class Form1 : Form
        {
            BackgroundWorker _bgWorker;
            int count;
    
            public Form1() 
            {
                InitializeComponent();
    
                _bgWorker = new BackgroundWorker();
                _bgWorker.WorkerReportsProgress = true;
                _bgWorker.WorkerSupportsCancellation = true;
                _bgWorker.DoWork += _bgWorker_DoWork; ;
                _bgWorker.ProgressChanged += _bgWorker_ProgressChanged; ;
                _bgWorker.RunWorkerCompleted += _bgWorker_RunWorkerCompleted;
                timer1.Enabled = true;
            }
                    
            private void toolStripButtonStart_Click(object sender, EventArgs e)
            {            
                count = 0;
                _bgWorker.RunWorkerAsync();
            }
    
            private void toolStripButtonStop_Click(object sender, EventArgs e)
            {
                _bgWorker.CancelAsync();
    
                // 加上这三行代码就会导致不能触发_bgWorker_RunWorkerCompleted,_bgWorker.IsBusy一直未true阻塞在这个地方
                while (_bgWorker.IsBusy)
                {
                    Thread.Sleep(100);
                }            
            }
            
            private void _bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                System.Windows.Forms.MessageBox.Show("Test Done");
            }
    
            private void _bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                toolStripLabelIsRunning.Text = count++.ToString();
            }
    
            private void _bgWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                BackgroundWorker bgWorker = sender as BackgroundWorker;
                bool boolFlag=false;
                while (!bgWorker.CancellationPending)
                {
                    Thread.Sleep(100);
                    boolFlag = !boolFlag;
                    if (boolFlag)
                    {
                        bgWorker.ReportProgress(1);
                    }
                }
                Debug.WriteLine("Successfully Exite");
            }       
        }

    在一个简单的form中添加一个BackgroundWorker,并注册DoWork,ProgressChanged,RunWorkerCompleted事件

    在如图中toolStripButtonStop_Click 界面响应事件函数中如果没有加以下三行代码一切运行正常,Start后正常运行,Stop后正常的推出并触发RunWorkerCompleted事件,

               while (_bgWorker.IsBusy)

               {

                   Thread.Sleep(100);

                }

    但是如果toolStripButtonStop_Click 界面响应事件函数中加入了这三行代码则RunWorkerCompleted事件永远都不会被触发,_bgWorker.IsBusy永远都是true导致toolStripButtonStop_Click 界面响应事件函数无法退出(实际上_bgWorker_DoWork函数早就已经退出了,在输出窗口中可以看到_bgWorker_DoWork函数退出前运行了 Debug.WriteLine("Successfully Exite"); 输出的Successfully Exite字符串


    请教各位大神,这是为什么???

  • 相关阅读:
    as3工程和flex工程的区别
    Timer的repeatCount和currentCount的区别
    mouseChildren为false后,
    flex编译时,会把trace语句也编译进去
    stage和root的区别
    flex编译时,会把trace语句也编译进去
    水瓶座(1.202.19)更多星座运程
    如何更改titleWindow组件上的title字体大小?
    转贴关于AsWing和MXML 选项
    Eclipse中的文本编辑器使用技巧
  • 原文地址:https://www.cnblogs.com/lyh523329053/p/9824660.html
Copyright © 2011-2022 走看看