使用C#Windows窗体线程在运行时更改GUI



我正在尝试制作一个服务器面板来检查我的三台服务器是否在线。如果有3个远程服务器在运行,想知道它们是否在线。我要做的是三个线程,它们正在检查1台服务器的状态并对结果采取行动。

class ServerChecker
{
    private byte firstIP, secondIP, thirthIP, fourthIP;
    private string message;
    private Boolean status;
    private Ping ping = new Ping();
    //Zet de waardes van de IP gelijk aan die van de class. En gaat daarna de server pingen.
    public Boolean checkConnection(byte first, byte second, byte thirth, byte fourth)
    {
        this.firstIP = first;
        this.secondIP = second;
        this.thirthIP = thirth;
        this.fourthIP = fourth;
        return connection();
    }
    //Stuurt een Ping naar de server en bekijkt de response en reageert op de response
    private Boolean connection()
    {
        PingReply reply;
        try
        {
            reply = ping.Send(new IPAddress(new byte[] { firstIP, secondIP, thirthIP, fourthIP }), 3000);
            //Als er een correct response is server online
            if (reply.Status == IPStatus.Success)
            {
                this.status = true;
                this.message = "    De Server is online...";
            }
            //Als er een incorrecte response is server offline
            else
            {
                this.status = false;
                this.message = "    De Server is Offline...";
            }
        }
        //Als er iets fout gaat vang de fout op en geef een message.
        catch (Exception)
        {
            this.status = false;
            this.message = "    Er is iets Fout gegaan";
        }
        return this.status;
    }
    public string getMessage()
    {
        return this.message;
    }
}

这是我的表格

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Server.ProgramForm
{
    public partial class Form1 : Form
    {
        private Image WebsiteServerImage,DatabaseServerImage,ApplicatieServerImage,WebsiteImage,DatabaseImage,ApplicatieImage;
        private Engine.ThreadHandler thread;
        public Form1()
        {
            InitializeComponent();
            InitializeImages();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
              this.WebsiteServer_Image.Image = WebsiteServerImage;
              this.DatabaseServer_Image.Image = DatabaseServerImage;
              this.ApplicatieServer_Image.Image = ApplicatieServerImage;
        }
        private void InitializeImages()
        {
            setWebsiteServerImageDisabled();
            setDatabaseServerImageDisabled();
            setApplicatieServerImageDisabled();
        }
        public void setWebsiteServerImage(Boolean Status)
        {
                switch (Status)
                {
                    case true: WebsiteServer_Image.Image = global::Server.Properties.Resources.web_online;
                        break;
                    case false: WebsiteServer_Image.Image = global::Server.Properties.Resources.web_offline;
                        break;
                }
        }
        public void setWebsiteServerImageDisabled()
        {
            WebsiteServerImage = global::Server.Properties.Resources.web_error;
        }
        public void setDatabaseServerImage(Boolean Status)
        {
            switch (Status)
            {
                case true: DatabaseServerImage = global::Server.Properties.Resources.db_online;
                    break;
                case false: DatabaseServerImage = global::Server.Properties.Resources.db_offline;
                    break;
            }
        }
        public void setDatabaseServerImageDisabled()
        {
            DatabaseServerImage = global::Server.Properties.Resources.db_error;
        }
        public void setApplicatieServerImage(Boolean Status)
        {
            switch (Status)
            {
                case true: ApplicatieServerImage = global::Server.Properties.Resources.dev_online;
                    break;
                case false: ApplicatieServerImage = global::Server.Properties.Resources.dev_offline;
                    break;
            }
        }
        public void setApplicatieServerImageDisabled()
        {
            ApplicatieServerImage = global::Server.Properties.Resources.dev_error;
        }
    }
}

这是我的程序(主(

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Server.Engine
{
    class Program
    {
        private static ProgramForm.Form1 form = new ProgramForm.Form1();
        private static ThreadHandler thread;
        private static Boolean websiteStatus;
        public static void Main()
        {
            //Start eerst de Threads voordat je Form laat zien
            thread = new ThreadHandler(form);
            thread.startWebsiteThread();
            thread.startDatabaseThread();
            form.ShowDialog();
        }
    }
}

ThreadHandler类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Server.Engine
{
    class ThreadHandler
    {
        private Thread DatabaseServerThread,ApplicatieServerThread,WebsiteServerThread;
        private Checkers.ServerChecker ServerChecker=new Checkers.ServerChecker();
        private ProgramForm.Form1 form;
        private Boolean DatabaseServerStatus, WebsiteServerStatus, ApplicatieServerStatus;
        private System.Threading.Timer DatabaseServerTimer;
        //Constructor
        public ThreadHandler(ProgramForm.Form1 temp)
        {
            this.form = temp;
        }
        //Start de DatabaseServer Thread
        public void startDatabaseThread()
        {
            this.DatabaseServerThread = new Thread(new ThreadStart(DatabaseThreadHandling));
            this.DatabaseServerThread.Start();
        }
        //Regelt de handelingen van de Database Thread
        private void DatabaseThreadHandling()
        {
                if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() == true)
                {
                    //Geeft true terug als de ping slaagd.
                    this.DatabaseServerStatus = ServerChecker.checkConnection(145, 24, 222, 103);
                    form.setDatabaseServerImage(this.DatabaseServerStatus);
                }
                else
                {
                    form.setDatabaseServerImageDisabled();
                }
        }
        //Start de WebsiteServer Thread
        public void startWebsiteThread()
        {
            this.WebsiteServerThread = new Thread(new ThreadStart(WebsiteThreadHandling));
            this.WebsiteServerThread.Start();
        }
        private void WebsiteThreadHandling()
        {
                if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() == true)
                {
                    //Geeft true terug als de ping slaagd.
                    this.WebsiteServerStatus = ServerChecker.checkConnection(145, 24, 222, 118);
                    form.setWebsiteServerImage(this.WebsiteServerStatus);
                }
                else
                {
                    form.setWebsiteServerImageDisabled();
                }
        }
        //Start de ApplicatieServer Thread
        public void startApplicatieThread()
        {
        }
    }
}

我的问题是,这只会更新我的UI。它不会改变。我不知道该怎么解决这个问题。我试着在不踩UI线程的情况下运行代码,但这是一个更糟糕的想法,它给UI带来了很大的滞后。我想做的是,当服务器发生变化时。将图像和标签从联机更改为脱机。但我想运行这个进程来检查另一个线程上服务器的状态(总共3个(。当我试图更改图片时,我遇到了一个错误,我无法更改其他线程上的控件。我发布的代码只运行一个。。有人想出了解决这个问题的绝妙办法。

form.Invoke(new Action(() => label.Text = "offline"));

或者类似的

我解决了这个问题。我在form_load中创建了一个计时器,并创建了重新绘制函数。我将Controls设置为这样的变量。This.picturebox.Image=tempImage其中tempImage是图像的类型。我从另一个线程更改tempImage,而不让线程直接更改控制器。如果你想要一个样本,你可以随时留下一条消息

相关内容

  • 没有找到相关文章

最新更新