c# - 创建新线程时,不对 GUI 进行更改 (C#)

标签 c# .net windows

在一些帮助下,我设法创建了一个新线程,尽管该方法似乎在执行,但该方法的条件要么使绿灯亮起,要么使红灯亮起,尽管在没有新线程的情况下运行该方法(Check1..etc)线程更改会反射(reflect)在 GUI 上(例如出现红灯/绿灯),但是当创建新线程并运行该方法时,更改不会反射(reflect)在表单/GUI 上。

// Method / Action to start the checks         private void StartChecks_Click(object sender, EventArgs e)
        {

            Thread t = new Thread(
               o =>
               {
                   InitChecks();
               });
            t.Start();
        }

// Check1 public void Check1()
        {

            // lets grabs the info from the config!
            var lines = File.ReadAllLines("probe_settings.ini");
            var dictionary = lines.Zip(lines.Skip(1), (a, b) => new { Key = a, Value = b })
                                  .Where(l => l.Key.StartsWith("#"))
                                  .ToDictionary(l => l.Key, l => l.Value);

            // lets set the appropriate value for this check field
            label1.Text = dictionary["#CheckName1"];

            // lets define variables and convert the string in the dictionary to int for the sock.connection method!

            int portno1;
            int.TryParse(dictionary["#PortNo1"], out portno1);

            // Convert hostname to IP, performance issue when using an invalid port on a hostname using the TcpClient class! 
            IPAddress[] addresslist = Dns.GetHostAddresses(hostname2);

            foreach (IPAddress theaddress in addresslist)
            {
                // Attempt to create socket and connect to specified port on host
                TcpClient tcP = new System.Net.Sockets.TcpClient();
                try
                {
                    tcP.ReceiveTimeout = 1;
                    tcP.SendTimeout = 1;
                    tcP.Connect(theaddress, portno1);
                    displayGreen1();
                    tcP.Close();
                }
                catch
                {
                    displayRed1();
                }
            }

        }

// Change the lights when the condition is met

        public void displayGreen1()
        {
            pictureBox2.Visible = false;
            pictureBox1.Visible = true;
        }

        private void displayRed1()
        {
            pictureBox2.Visible = true;
            pictureBox1.Visible = false;
        }

最佳答案

这就是 WinForms 的设计方式。您不能从另一个线程进行更改。 解决方案通常是使用异步委托(delegate)。

首先添加这个声明

 public delegate void MyDelegate1 ();

 public delegate void MyDelegate2 (); 

然后当你在另一个线程中时你应该这样做:

MyDelegate1 d1= new MyDelegate1 (displayGreen1);

 this.BeginInvoke(d1);

 MyDelegate2 d2= new MyDelegate2 (displayRed1);

 this.BeginInvoke(d2);

关于c# - 创建新线程时,不对 GUI 进行更改 (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8544539/

相关文章:

c# - 重新排序列表中项目的正确方法(nhibernate)

c# - MySQL 站点搜索中参数化查询的问题

.net - 可空类型的 LINQ 聚合行为

c# - Visual Studio 2013 错误列表未显示失败构建的错误

ios - 我应该为 ios 客户端启用 cors 吗?

python - 读取 Windows 文件而不阻止其他进程写入该文件

windows - 从 WSL 启动资源管理器

c# - 使用 xaml 自定义 View 中的嵌套元素

c# - 使用 Entity Framework 从 2 个表返回数据

检查 VirtualAlloc 是否返回了 VirtualAddress