C# 从后台线程更新datagridview

C# 从后台线程更新datagridview,c#,multithreading,datagridview,ping,C#,Multithreading,Datagridview,Ping,我正在从UI调用方法,方法需要很长时间才能完成 因此,它冻结了用户界面,并阻止用户了解正在发生的事情 我正在尝试更新“datagridview”中的列(状态),以便在UI上反馈用户,让他知道哪个ip已连接,哪个尚未连接,但正如我在UI冻结之前所说的,直到方法完成为止 其中一个建议是使用线程,我这样做了,但我的问题没有解决,所以我是否正确地做了一些事情 public void PatchUpdates() { try { fo

我正在从UI调用方法,方法需要很长时间才能完成 因此,它冻结了用户界面,并阻止用户了解正在发生的事情

我正在尝试更新“datagridview”中的列(状态),以便在UI上反馈用户,让他知道哪个ip已连接,哪个尚未连接,但正如我在UI冻结之前所说的,直到方法完成为止

其中一个建议是使用线程,我这样做了,但我的问题没有解决,所以我是否正确地做了一些事情

    public void PatchUpdates()
    {
        try
        {
            foreach (DataGridViewRow OfficeListRow in DGV_OfficeList.Rows)
            {
                string OfficeIPAddress = OfficeListRow.Cells[3].Value.ToString();

                foreach (DataGridViewRow FileListRow in DGV_FileList.Rows)
                {
                    string SoruceFileNamePath = FileListRow.Cells[4].Value.ToString();
                    string DestinationFileNamePath = @"\\" + OfficeIPAddress + @"\usb1_1\test\" + Path.GetFileName(SoruceFileNamePath);

                    Thread foregroundthread = new Thread(() => CheckOffice(OfficeIPAddress));
                    foregroundthread.Start();

                    //check if connection to remote server is available
                    if (CheckOffice(OfficeIPAddress) == 1)
                    {
                        DGV_OfficeList[4, DGV_OfficeList.CurrentCell.RowIndex].Value = "Connected";
                        //file.copy(sorucefilenamepath, destinationfilenamepath, true); //copy files...
                    }
                    else if (CheckOffice(OfficeIPAddress) == 0)
                    {
                        DGV_OfficeList[4, DGV_OfficeList.CurrentCell.RowIndex].Value = "disconnected";
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
平法

    public int CheckOffice(string _ipAddress)
    {
        int timeout = 120;
        string data = "PingTestData";
        byte[] buffer = Encoding.ASCII.GetBytes(data);

        Ping PingSender = new Ping();
        PingOptions options = new PingOptions();

        options.DontFragment = true;

        PingReply reply = PingSender.Send(_ipAddress, timeout, buffer, options);

        if (reply.Status == IPStatus.Success)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

目前,您要调用CheckOffice三次,一次在新线程中,两次在GUI线程中。未使用在新线程中计算的返回值

尝试替换:

                Thread foregroundthread = new Thread(() => CheckOffice(OfficeIPAddress));
                foregroundthread.Start();

                //check if connection to remote server is available
                if (CheckOffice(OfficeIPAddress) == 1)
                {
                    DGV_OfficeList[4, DGV_OfficeList.CurrentCell.RowIndex].Value = "Connected";
                    //file.copy(sorucefilenamepath, destinationfilenamepath, true); //copy files...
                }
                else if (CheckOffice(OfficeIPAddress) == 0)
                {
                    DGV_OfficeList[4, DGV_OfficeList.CurrentCell.RowIndex].Value = "disconnected";
                }


这使用一个任务而不是显式线程,如果您只想启动异步逻辑,然后使用BeginInvoke使用GUI线程将更新应用到GUI,这是更好的做法,否则这将导致DataGridView控件出现线程不安全的问题。BeginInvoke应该在Winforms窗口或控件上定义,或者您可以查看Dispatcher。

首先非常感谢您的帮助Task.Run()'在以下情况下不起作用,因为我使用的是dot net 4,当我阅读有关此问题的搜索时,我发现可以使用'Task.Factory.StartNew',但它有很大的风险>>再次感谢您的帮助教给我
        var task = Task.Run(() =>
            {
                var result = CheckOffice(OfficeIPAddress);

                this.BeginInvoke((Action)(() =>
                {
                    if (result == 1)
                    {
                        DGV_OfficeList[4, DGV_OfficeList.CurrentCell.RowIndex].Value = "Connected";
                        //file.copy(sorucefilenamepath, destinationfilenamepath, true); //copy files...
                    }
                    else if (result == 0)
                    {
                        DGV_OfficeList[4, DGV_OfficeList.CurrentCell.RowIndex].Value = "disconnected";
                    }
                }));
            }
        );