【问题标题】:start method within new thread新线程中的启动方法
【发布时间】:2023-03-27 20:05:01
【问题描述】:

我有方法 PatchUpdates 调用 CheckConnection 方法来检查与远程 pc 的连接,如果为真,那么它将返回到用户界面的第一个方法并做一些其他的事情

我搜索并发现我需要使用线程所以我正在创建新线程 但我的应用程序挂起并停止,什么也没发生

请问我做错了什么?

谢谢

        public void PatchUpdates()
        {
            try
            {
                foreach (DataGridViewRow OfficeListRow in DGV_OfficeList.Rows)
                {
                    string vIPAddress;
                    string vSoruceFilePath;
                    int RowNum;

                    foreach (DataGridViewRow FileListRow in DGV_FileList.Rows)
                    {

                        Thread thrd = new Thread(new System.Threading.ThreadStart(PatchUpdates));
                        thrd.Start();

                        vIPAddress = OfficeListRow.Cells[1].Value.ToString();
                        vSoruceFilePath = FileListRow.Cells[4].Value.ToString();
                        RowNum = OfficeListRow.Index;
                        ///Check the connection to pc
                        if (CheckConnection(vIPAddress) == true)
                        {
                            //MessageBox.Show(vIPAddress + " Pingable ");
                            DGV_OfficeList[2, RowNum].Value = "Online";
                            OfficeListRow.DefaultCellStyle.BackColor = Color.LightGreen;
                        }
                        else
                        {
                            //MessageBox.Show(vIPAddress + " Not Pingable ");
                            DGV_OfficeList[2, RowNum].Value = "Offline";
                            OfficeListRow.DefaultCellStyle.BackColor = Color.LightCyan;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



    public static bool CheckConnection(string IPAddress)
    {
        bool vPingable = false;

        try
        {
            Ping png = new Ping();
            PingReply PngReply = png.Send(IPAddress);
            vPingable = PngReply.Status == IPStatus.Success;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }
        return vPingable;
    }

【问题讨论】:

  • 使用异步 Ping,以便您可以同时执行多个 ping。
  • thrd.Start();上下断点运行代码。你打断点多少次?你认为这是为什么?

标签: c# multithreading .net-3.5


【解决方案1】:

您正在从 PatchUpdates() 方法中将 PatchUpdates 传递给 ThreadStart 委托。

Thread thrd = new Thread(new System.Threading.ThreadStart(PatchUpdates));
thrd.Start();

这意味着PatchUpdates() 方法在新的第二个线程上重新开始,在新的第三个线程上重新开始,在新的第四个线程上重新开始,依此类推。 ..

基本上,您正在启动无限的新线程(只要DGV_FileList.Rows 中有项目),最终会消耗您的所有资源。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 2013-10-03
    • 1970-01-01
    • 2023-02-15
    • 2021-01-13
    • 2016-01-23
    相关资源
    最近更新 更多