遍历IP地址时UI不更新

时间:2020-04-27 04:58:28

标签: c# winforms loops user-interface

我有一个简单的工作要做,单击按钮,它将获得一个范围内的所有网络IP地址,遍历它们并将活动对象放入列表。在执行该过程时,将显示一个面板,在该面板上将显示正在检查的IP地址。代码运行良好,但是形式挂起,应用程序不响应,即使面板未显示,IP地址也未显示。怎么做? 我的代码是:

private void btnAutoSearch_Click(object sender, EventArgs e)
    {
        panel_Search.Visible = true; // Not Working
        Cursor.Current = Cursors.WaitCursor;

        string ipBase = getIPAddress();
        string[] ipParts = ipBase.Split('.');
        ipBase = ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + ".";



        for (int i = 1; i < 255; i++)
        {
            string ip = ipBase + i.ToString();
            Ping p = new Ping();                
            PingReply pingresult = p.Send(ip, 100);
            if (pingresult.Status == IPStatus.Success)
            {
                lstIPs.Add(ip);
                lblConnecting.Text = ip;  // Not Working

            }

        }


        GridConn.Rows.Clear();
        foreach (string s in lstIPs)
        {
            Device obj = new Device();
            obj.IPAddress = s;
            lblConnecting.Text = s;
            int vSMSLimit = 0;
            int.TryParse(txtSMSLimit.Text, out vSMSLimit);
            obj.SMSLimit = 0;
            if (obj.ConnectToHostServer())
            {
                obj.SendConnectionMessage();
                obj.ReceiveConnectionMessage();
                MyDevices.lst.Add(obj);
                GridConn.Rows.Add(true, obj.IPAddress, obj.PhoneModel, obj.PhoneStatus, obj.SoftwareVersion);
            }
        }


         Cursor.Current = Cursors.Default;
        panel_Search.Visible = false;
    }

3 个答案:

答案 0 :(得分:0)

问题在于,必须从“主”线程(始终称为UI线程)更新UI,但是如果对此线程进行其他处理,则UI本身将被锁定。因此,您应该将长时间运行的进程放在另一个线程上。

这将导致问题,但是该线程无法更新UI,因为它不是Main / UI线程。因此,您必须自己调用UI线程进行更新。

幸运的是,使用C#中的BackgroundWorker类有一种简单的方法可以帮助您。但是您需要分离出UI和后台任务。

//Define worker
BackgroundWorker myBGWorker;


//Initalise worker and start it from your button
private void btnAutoSearch_Click(object sender, EventArgs e)
{
    panel_Search.Visible = true; // Not Working
    Cursor.Current = Cursors.WaitCursor;

    myBGWorker = new BackgroundWorker()

    //This method will execute your processing on a background thread
    myBGWorker.DoWork += new DoWorkEventHandler(bgw_DoWork);

    //This method will execute when your background worker finishes
    //It runs on the Main/UI thread
    myBGWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);

    //This method will execute when the background thread wants to update the progress
    //It runs on the Main/UI Thread
    myBGWorker.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);

    //Tell it we will be reporting progress
    myBGWorker.WorkerReportsProgress = true;

    //Start!
    myBGWorker.RunWorkerAsync()
}

private void bgw_DoWork(object sender, DoWorkEventArgs e)  
{  
        string ipBase = getIPAddress();
        string[] ipParts = ipBase.Split('.');
        ipBase = ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + ".";

        for (int i = 1; i < 255; i++)
        {
            string ip = ipBase + i.ToString();
            Ping p = new Ping();                
            PingReply pingresult = p.Send(ip, 100);
            if (pingresult.Status == IPStatus.Success)
            {
                lstIPs.Add(ip);

                //Below reports the progress.  The number shouuld represent the percentage of process, the object can be anything you want
                double percentDone = (100.0 / 255.0) * i;            
                e.ReportProgress(Convert.ToInt32(percentDone), ip);
            }

        }
}  

private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    lblConnecting.Text = e.UserState as string;  
}


private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
{
    if (e.Cancelled) 
        MessageBox.Show("Operation was canceled");  
    else if (e.Error != null) 
        MessageBox.Show(e.Error.Message);  
    else 
    {
       GridConn.Rows.Clear();
        foreach (string s in lstIPs)
        {
            Device obj = new Device();
            obj.IPAddress = s;
            lblConnecting.Text = s;
            int vSMSLimit = 0;
            int.TryParse(txtSMSLimit.Text, out vSMSLimit);
            obj.SMSLimit = 0;
            if (obj.ConnectToHostServer())
            {
                obj.SendConnectionMessage();
                obj.ReceiveConnectionMessage();
                MyDevices.lst.Add(obj);
                GridConn.Rows.Add(true, obj.IPAddress, obj.PhoneModel, obj.PhoneStatus, obj.SoftwareVersion);
            }
        }

        Cursor.Current = Cursors.Default;
        panel_Search.Visible = false;
    }
}

答案 1 :(得分:0)

桌面应用程序使用一个线程来处理UI,事件处理程序在UI线程上同步运行,这就是表单挂起的原因,因为它无法在事件处理程序运行时处理其他UI交互。

最佳做法是事件处理程序在很短的时间内占用UI线程。

当由于用户交互而需要执行长时间运行的任务时,应该在另一个线程中执行该任务,而不应该将UI线程用于长时间运行的任务。

您可以使用BackgroundWorker将任务移至另一个线程,但是最好使用异步事件处理程序。

例如:

private async void myButton_Click(object sender, EventArgs e)
{
    await PerformLongRunningTaskAsync();
    //TODO: update UI after completing task

    await Task.Run(() => PerformLongRunningTaskSynchronously());
    //TODO: update UI after completing task;
}

private async Task PerformLongRunnigTaskAsync() {
   //TODO: implement this async method
}

private void PerformLongRunningTaskSynchronously() {
  //TODO: implement this synchronus method
}

您的代码应如下所示:

private async void btnAutoSearch_Click(object sender, EventArgs e)
    {
        panel_Search.Visible = true; // Not Working
        Cursor.Current = Cursors.WaitCursor;

        string ipBase = getIPAddress();
        string[] ipParts = ipBase.Split('.');
        ipBase = ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + ".";



        for (int i = 1; i < 255; i++)
        {
            string ip = ipBase + i.ToString();
            Ping p = new Ping();                
            PingReply pingresult = await p.SendPingAsync(ip, 100);
            if (pingresult.Status == IPStatus.Success)
            {
                lstIPs.Add(ip);
                lblConnecting.Text = ip;  // Not Working

            }

        }


        GridConn.Rows.Clear();
        foreach (string s in lstIPs)
        {
            Device obj = new Device();
            obj.IPAddress = s;
            lblConnecting.Text = s;
            int vSMSLimit = 0;
            int.TryParse(txtSMSLimit.Text, out vSMSLimit);
            obj.SMSLimit = 0;
            if (await Task.Run(() => obj.ConnectToHostServer())
            {
                await Task.Run(() => obj.SendConnectionMessage());
                await Task.Run(() => obj.ReceiveConnectionMessage());
                MyDevices.lst.Add(obj);
                GridConn.Rows.Add(true, obj.IPAddress, obj.PhoneModel, obj.PhoneStatus, obj.SoftwareVersion);
            }
        }


         Cursor.Current = Cursors.Default;
        panel_Search.Visible = false;
    }

答案 2 :(得分:0)

这是我的工作方式,这要归功于Brian Rogers,

 private void btnAutoSearch_Click(object sender, EventArgs e)
    {

        backgroundWorker1.WorkerSupportsCancellation = true;


        if (backgroundWorker1.IsBusy != true)
        {
            // Start the asynchronous operation.
            backgroundWorker1.RunWorkerAsync();
        }

    }

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        BackgroundWorker worker = sender as BackgroundWorker;

        for (int j = 1; j <= 10; j++)
        {
            if (worker.CancellationPending == true)
            {
                e.Cancel = true;
                break;
            }
            else
            {
                Cursor.Current = Cursors.WaitCursor;
                panel_Search.Location = new Point(380, 72);
                // Perform a time consuming operation and report progress.
                string ipBase = getIPAddress();
                string[] ipParts = ipBase.Split('.');
                ipBase = ipParts[0] + "." + ipParts[1] + "." + ipParts[2] + ".";



                for (int i = 1; i < 255; i++)
                {
                    string ip = ipBase + i.ToString();
                    Ping p = new Ping();                                                
PingReply pingresult = p.Send(ip, 100);
                    if (pingresult.Status == IPStatus.Success)
                    {
                        lstIPs.Add(ip);                            
                        lblConnecting.Text = ip;
                        //listBox1.Items.Add(ip);                
                    }

                }




                GridConn.Rows.Clear();
                foreach (string s in lstIPs)
                {
                    Device obj = new Device();
                    obj.IPAddress = s;
                    lblConnecting.Text = s;
                    int vSMSLimit = 0;
                    int.TryParse(txtSMSLimit.Text, out vSMSLimit);
                    obj.SMSLimit = 0;
                    if (obj.ConnectToHostServer())
                    {
                        obj.SendConnectionMessage();
                        obj.ReceiveConnectionMessage();
                        MyDevices.lst.Add(obj);
                        GridConn.Rows.Add(true, obj.IPAddress, obj.PhoneModel, obj.PhoneStatus, obj.SoftwareVersion);
                    }
                }

                Cursor.Current = Cursors.Default;
                panel_Search.Location = new Point(333, 252);

            }
        }
    }