visual studio c#label循环更新

时间:2018-02-21 09:27:29

标签: c# visual-studio loops colors label

我希望有人可以帮助我,我有一个为每个实例生成标签的循环,这还包括一个更改标签颜色的if语句。但是,当循环再次开始时,标签就在那里,但它们不会改变if语句中的任何颜色。我将在下面粘贴我的代码;我希望有人能帮助我解决我在更改标签颜色方面的问题

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinSCP;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Configuration;

namespace VIACamMonitoring
{
    public partial class Form1 : Form
    {          
        private static Form1 instancef;

        string tempString = "";
        List<string> quick = new List<string>();
        List<TextBox> list = new List<TextBox>();

        string[] cat2;
        string[] cat;

        int location = 99;
        int location2 = 102;

        int hello = new int();   

        public Form1()
        {
            InitializeComponent();
            instancef = this;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 300000; //1800000;
            timer.Elapsed += timer_Elapsed;
            timer.Start();

        }  

        static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            string[] cat2;
            string[] cat;
            List<string> quick = new List<string>();
            List<TextBox> list = new List<TextBox>();
            List<Label> labellist = new List<Label>();
            int location = 99;
            int location2 = 102;
            byte[] buffer = new byte[300];  

            StreamReader textread = new StreamReader("config.txt");
            string AllData = textread.ReadToEnd();
            string[] ssize = AllData.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string line in ssize)
            {    
                cat2 = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                string instance = cat2[0];
                string instance2 = cat2[1];
                string instance3 = cat2[2];
                string instance4 = cat2[3];
                string instance5 = cat2[4];    

                quick.Add(instance + "," + instance2 + "," + instance3 + "," + instance4 + "," + instance5);    
            }  

            for (int i = 0; i < quick.Count; i++)
            {
                char[] delimiterChars = { ',', '\t' };
                string happy = quick[i];
                cat = happy.Split(delimiterChars);

                string element = cat[0];
                string element1 = cat[1];
                string element2 = cat[2];
                string element3 = cat[3];
                string element4 = cat[4];

                list.Add(new TextBox());
                list[i].Text = element;
                list[i].Location = new System.Drawing.Point(53, location);
                list[i].Size = new System.Drawing.Size(441, 22);

                labellist.Add(new Label());
                labellist[i].Text = "Status";
                labellist[i].Location = new System.Drawing.Point(520, location2);
                labellist[i].Size = new System.Drawing.Size(48, 17);                

                instancef.Invoke((MethodInvoker)delegate { instancef.Controls.Add(list[i]); });

                SessionOptions sessionOptions = new SessionOptions
                {
                     Protocol = Protocol.Sftp,
                     HostName = element1,
                     UserName = element2,
                     Password = element3,
                     SshHostKeyFingerprint = element4
                };

                try
                {
                    using (Session session = new Session())
                    {
                        session.Open(sessionOptions);

                        if (session.Opened == true)
                        {
                            Console.WriteLine(element + " - " + "Connection Opened");                               
                            labellist[i].BackColor = Color.Green; 
                        }
                        session.Close();
                    }    
                 }
                 catch (Exception ex)
                 {
                    Console.WriteLine(element + " Error");
                    labellist[i].BackColor = Color.Red;

                    UdpClient udpClient = new UdpClient();
                    udpClient.Connect(element1, 50);
                    Console.WriteLine(element1 + "REBOOT");
                    Byte[] senddata = Encoding.ASCII.GetBytes("reboot");
                    udpClient.Send(senddata, senddata.Length);
                    udpClient.Send(senddata, senddata.Length);
                    udpClient.Send(senddata, senddata.Length);
                }
                // Console.WriteLine(element1);
                Console.WriteLine(element + " - Connection Closed");
                instancef.Invoke((MethodInvoker)delegate { instancef.Controls.Add(labellist[i]); });
                location = location + 60;
                location2 = location2 + 60;
            }    
        } 
    }
}

我使用了一种非常差的方法来解决我的问题,这是

 if(i == 0)
                {

                    instancef.Invoke((MethodInvoker)delegate { instancef.Controls.Clear(); });
                }

然而,现在我输入的其他不属于循环的标签现在也消失了,有没有办法保留它们,一旦循环开始就不会消失

1 个答案:

答案 0 :(得分:0)

您无法在GUI线程上执行冗长的操作,例如连接到远程服务器。

阻止窗口更新。