multithreading - Change GUI on runtime with threads C# Windows Form -


i'm trying make serverpanel check if 3 servers online. if got 3 remote servers running , want know if online. i'm trying 3 threads checking status of 1 server , act on result.

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 server online             if (reply.status == ipstatus.success)             {                 this.status = true;                 this.message = "    de server online...";             }             //als er een incorrecte response server offline             else             {                 this.status = false;                 this.message = "    de server offline...";             }         }         //als er iets fout gaat vang de fout op en geef een message.         catch (exception)         {             this.status = false;             this.message = "    er iets fout gegaan";         }         return this.status;     }     public string getmessage()     {         return this.message;     } } 

this form

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

this program(main)

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 class:

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()         {         }     } } 

my problem update ui ones. , not change. i'm out of idea's solve problem. tryed run code without treads on ui thread worse idea , gives ui mayor lagg. trying is. when serverchange happend. change image , label online offline. want run process check status of server on other thread(3 total). when trying change pictures error cant change controls on other thread. code posted run ones.. got brilliant idea how solve problem.

form.invoke(new action(() => label.text = "offline")); 

or effect


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -