此BackgroundWorker当前正忙,并且在PING Ip地址时无法同时运行多个任务

时间:2016-01-29 16:30:55

标签: c#

我的程序执行PING时遇到使用ProgressBar的问题。错误说"此BackgroundWorker当前正忙,无法同时运行多个任务"。谁能帮我 ?这就是我所做的

private void ProsesSemuaKamera()
        {

            Stopwatch watch = Stopwatch.StartNew();

            Ping ping = new Ping();
            PingReply pingreply;

            BackgroundWorker BWKamera = new BackgroundWorker();

            OleDbConnection kon = new OleDbConnection(koneksi);
            OleDbCommand command = kon.CreateCommand();
            kon.Open();
            string selecturl = "select * from datakamera";
            command.CommandText = selecturl;
            OleDbDataReader bacadata = command.ExecuteReader();


            /* PING Kamera IP */

            while (bacadata.Read())
            {
                pingreply = ping.Send(bacadata["ipadd"].ToString());
                BWKamera.WorkerReportsProgress = true;
                BWKamera.DoWork += BWKamera_DoWork;
                BWKamera.ProgressChanged += BWKamera_ProgressChanged;

                BWKamera.RunWorkerAsync(); //error exists here

                if (pingreply.Status == IPStatus.Success)
                {
                    listBox1.Items.Add(bacadata["namakamera"].ToString());

                    if (CaptureSemuaKamera == null)
                    {
                        try
                        {
                            CaptureSemuaKamera = new Capture(bacadata["urlkamera"].ToString());
                        }
                        catch (NullReferenceException ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }

                    if (CaptureSemuaKamera != null)
                    {
                        Application.Idle += new EventHandler(ProcessFrameSemuaKamera);
                    }


                }
                else if (pingreply.Status != IPStatus.Success)
                {
                    listBox2.Items.Add(bacadata["namakamera"].ToString());
                }

                watch.Stop();
                File.AppendAllText(@"D:\Dokumen\Alfon\TA Alfon\Waktu Eksekusi Ping.txt", "Waktu eksekusi ping " + DateTime.Now + " :" + " " + watch.Elapsed.TotalMilliseconds.ToString() + Environment.NewLine);

            }       
            kon.Close();
       }

private void BWKamera_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i <= 1000; i++)
            {
                BWKamera.ReportProgress(i);
                Thread.Sleep(1000);
            }
        }

        private void BWKamera_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            ProgressBarKamera.Value = e.ProgressPercentage;
            StatusKamera.Text = "Please Wait...";
        }

        private void BWKamera_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            StatusKamera.Text = "Done !";
        }

1 个答案:

答案 0 :(得分:0)

因为它处于while循环中。我将假设第一个后台工作程序尚未完成,并且您要告诉它在同一个实例上再次运行。

OleDbDataReader bacadata = command.ExecuteReader();
BWKamera.WorkerReportsProgress = true;
BWKamera.DoWork += BWKamera_DoWork;
BWKamera.ProgressChanged += BWKamera_ProgressChanged;

BWKamera.RunWorkerAsync(bacadata);

然后后台工作者代码如下所示:

        private void BWKamera_DoWork(object sender, DoWorkEventArgs e)
    {

    OleDbDataReader bacadata = (OleDbDataReader)e.Argument;
    while (bacadata.Read())
        {
            pingreply = ping.Send(bacadata["ipadd"].ToString());