首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

运用BackgroundWorker后,点击按钮时还是有假死状态

2013-03-06 
使用BackgroundWorker后,点击按钮时还是有假死状态点击后,开始一会儿界面会有假死状态,过完这个假死状态后

使用BackgroundWorker后,点击按钮时还是有假死状态
点击后,开始一会儿界面会有假死状态,过完这个假死状态后面就流畅了,请问这个是什么问题。


BackgroundWorker bw = null;

        public Form3()
        {
            InitializeComponent();

            bw = new BackgroundWorker();
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i < 100; i++)
            {
                Thread.Sleep(1);
                if (bw.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }

                this.Invoke((MethodInvoker)delegate
                {
                    this.richTextBox1.Text += String.Format("第{0}次测试是否联网:{1}", i.ToString(), IsWWW("http://www.baidu.com?guid=" + Guid.NewGuid())) + Environment.NewLine;
                });
                Thread.Sleep(2000);
            }
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.richTextBox1.Text += "执行完毕";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.Enabled = false;
            this.button2.Enabled = true;

            bw.RunWorkerAsync();
        }



        private void button2_Click(object sender, EventArgs e)
        {
            this.button1.Enabled = true;
            this.button2.Enabled = false;

            bw.CancelAsync();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            RichTextBox textbox = (RichTextBox)sender;

            textbox.SelectionStart = textbox.Text.Length;
            textbox.ScrollToCaret();
        }


        private bool IsWWW(string url)
        { 
            try
            {
                new WebClient().DownloadString(url);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }


[解决办法]

    class Class1
    {
        Class1()
        {
            WaitHandle wh = new AutoResetEvent(true);
            Thread th = new Thread(new ParameterizedThreadStart(StartDoWork));
            th.Start(wh);
            wh.WaitOne();
            wh.Close();
            th.Abort();
        }

        void StartDoWork(object o)
        {
            var state = o as AutoResetEvent;
            bw_DoWork(this, null);
            Console.Write("执行完毕");
            state.Set();
        }



        void bw_DoWork(object sender, EventArgs e)
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(i);
            }
        }
    }


[解决办法]
是第一次建立webclient 这需要几秒不等,LZ可以移除webclient 就没有这个现象了 或者把webclient放在线程执行,返回结果后在invoke

热点排行