简体   繁体   English

Visual Studio C#标签更新循环

[英]visual studios c# label update in loop

I am hoping someone can help me, I have a loop which generates a label for each instance, this also includes an if statement which changes the colour of the label. 我希望有人可以帮助我,我有一个循环,可以为每个实例生成一个标签,其中还包括一个if语句,该语句可以更改标签的颜色。 However, when the loop begins again the labels are there but they don't change any colour within the if statement. 但是,当循环再次开始时,标签在那里,但它们不会更改if语句中的任何颜色。 I will paste my code below; 我将在下面粘贴我的代码; I am hoping someone can help me fix my issue in changing the colour of the label 我希望有人可以帮助我解决更改标签颜色的问题

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;
            }    
        } 
    }
}

i used a really poor method to solve my issue whihc is this 我用一种非常糟糕的方法来解决我的问题,这是

 if(i == 0)
                {

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

However now the rest of the labels i have inputted that are not part of the loop disappear now as well, is there a way to keep them, without them disappearing as soon as the loop starts 但是现在我输入的不属于循环的其余标签现在也消失了,有没有办法保留它们,而在循环开始时它们不会消失

You cannot do lengthy operations, like connecting to a remote server, on the GUI thread. 您无法在GUI线程上进行冗长的操作,例如连接到远程服务器。

That blocks window update. 阻止窗口更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM