c# - How to change back colour in listview dynamically -


i'm creating winforms app serves host monitoring in network. i'm pinging ip adresses , results of ping shown in listview (host ip address, delay time, description of host).

how can change dynamically colour of device shown in list view when device timeout?

this code of main form:

public partial class mainfrm : form {     private string xmlpath;     private checker checker;     private resourcemanager _rm;     openfiledialog ofd = new openfiledialog();      public mainfrm()     {         initializecomponent();          _rm = new resourcemanager("hostmonitor.resources", system.reflection.assembly.getexecutingassembly());          //assign events dataset         hosts.groups.tablenewrow += new datatablenewroweventhandler(groups_tablenewrow);         hosts.groups.groupsrowchanged += new hostsdata.groupsrowchangeeventhandler(groups_groupsrowchanged);         hosts.groups.groupsrowdeleted += new hostsdata.groupsrowchangeeventhandler(groups_groupsrowdeleted);          hosts.hosts.tablenewrow += new datatablenewroweventhandler(hosts_tablenewrow);         hosts.hosts.hostsrowchanged += new hostsdata.hostsrowchangeeventhandler(hosts_hostsrowchanged);           xmlpath = path.getdirectoryname(path.getdirectoryname(application.executablepath));         xmlpath = path.combine(path.getdirectoryname(xmlpath), "hosts.xml");          xmlpath=path.combine(path.getdirectoryname(application.executablepath), "hosts.xml");          hosts.readxml(xmlpath);          datarelation rel = new datarelation("hostsingroup", hosts.groups.columns["groupname"], hosts.hosts.columns["group"]);         hosts.relations.add(rel);          loaddata();          //start pinging         checker = new checker(comps, this);         checker.start();      }      void hosts_hostsrowchanged(object sender, hostsdata.hostsrowchangeevent e)     {         hostsdata.hostsrow row = (hostsdata.hostsrow)e.row;         foreach (listviewitem item in comps.items)         {                             if (item.tag == row)             {                 item.text = row.hostname;                 item.subitems[2].text = row.description;                 if ((item.group == null) || (item.group.header != row.group))                 {                     foreach (listviewgroup gr in comps.groups)                     {                         if (gr.header == row.group)                         {                             item.group = gr;                             break;                         }                      }                 }                 break;               }         }     }      void hosts_tablenewrow(object sender, datatablenewroweventargs e)     {         hostsdata.hostsrow row = (hostsdata.hostsrow)e.row;         listviewgroup group = null;         if (row.group!="")         {             foreach (listviewgroup gr in comps.groups)             {                 if (gr.header == row.group)                 {                     group = gr;                     break;                 }             }          }          listviewitem item = new listviewitem(group);         item.text = row.hostname;         item.tag = row;         item.subitems.add("");         item.subitems.add(row.description);         comps.items.add(item);     }      void groups_groupsrowdeleted(object sender, hostsdata.groupsrowchangeevent e)     {         hostsdata.groupsrow row = (hostsdata.groupsrow)e.row;         foreach (listviewgroup gr in comps.groups)         {             if (gr.tag == row)             {                 comps.groups.remove(gr);                 return;             }         }     }      void groups_groupsrowchanged(object sender, hostsdata.groupsrowchangeevent e)     {         hostsdata.groupsrow row = (hostsdata.groupsrow)e.row;         foreach (listviewgroup gr in comps.groups)         {             if (gr.tag == row)             {                 gr.header = row.groupname;                 return;             }         }     }      void groups_tablenewrow(object sender, datatablenewroweventargs e)     {                     listviewgroup gr = new listviewgroup();         hostsdata.groupsrow row = (hostsdata.groupsrow)e.row;         gr.header = row.groupname;         gr.tag = row;         comps.groups.add(gr);     }      private void loaddata()     {         comps.items.clear();         comps.groups.clear();          foreach (hostsdata.groupsrow group in hosts.groups)         {             listviewgroup gr = new listviewgroup();             gr.header = group.groupname;             gr.tag = group;             comps.groups.add(gr);                             foreach (hostsdata.hostsrow host in group.getchildrows("hostsingroup"))             {                 listviewitem ht = new listviewitem(gr);                 ht.text = host.hostname;                 ht.subitems.add("---");                 ht.subitems.add(host.description);                 ht.tag = host;                 comps.items.add(ht);             }         }     } 

this code of class checker:

public class checker {     private class checkhosteventargs:eventargs     {         private string _hostname;                     private listviewitem _item;                      public string hostname         {             { return _hostname;}         }                     public listviewitem item         {             { return _item;}                        }           public checkhosteventargs(string hostname, listviewitem item)         {             _hostname=hostname;             _item=item;         }     }      private delegate pingreply checkhost(checkhosteventargs e);     private delegate listviewitem[] getcompsdelegate();      private listview _list;     private mainfrm _frm;     private int asynccount = 0;     private thread _thread;     private resourcemanager _rm;      public checker(listview list,mainfrm form)     {         _rm = new resourcemanager("hostmonitor.resources", system.reflection.assembly.getexecutingassembly());         _list = list;         _frm = form;     }      public void start()     {         _thread = new thread(main);         _thread.start();     }       private void main()     {         try         {             while (true)             {                 oneloop();                 int64 time = environment.tickcount + config.interval;                 while (environment.tickcount < time)                 {                     thread.sleep(100);                 }             }         }         catch (threadabortexception)         {             return;                         }     }      private void oneloop()     {         foreach (listviewitem item in getcomps())         {             hostsdata.hostsrow host = (hostsdata.hostsrow)item.tag;              //eliminujemy nieaktywne hosty             if (host.hostname == "")             {                  continue;             }             if (!host.active)             {                 continue;             }             hostsdata.groupsrow group = (hostsdata.groupsrow)item.group.tag;             if (!group.active)             {                 continue;             }              //sprawdzamy aktywne hosty             lock (this)             {                 if (asynccount < config.maxasyncpings)                 {                     asynccount++;                     checkhost del = new checkhost(check);                     checkhosteventargs e = new checkhosteventargs(host.hostname, item);                     del.begininvoke(e, new asynccallback(checkcallback), item);                 }             }         }     }      private pingreply check(checkhosteventargs e)     {         ping p = new ping();         try         {             return p.send(e.hostname, config.timeout);         }         catch         {              return null;         }       }      private void checkcallback(iasyncresult asresult)     {         if (asresult.iscompleted)         {             if (_frm.invokerequired)             {                 asynccallback del = new asynccallback(checkcallback);                 _frm.invoke(del, asresult);             }             else             {                 try                 {                     asyncresult ar = (asyncresult)asresult;                     checkhost del = (checkhost)ar.asyncdelegate;                     pingreply r = del.endinvoke(asresult);                     listviewitem item = (listviewitem)ar.asyncstate;                     if (r != null)                     {                                                     switch (r.status)                         {                             case ipstatus.success:                                 item.subitems[1].text = string.format("{0}", r.roundtriptime);                                 break;                             default:                                 item.subitems[1].text = r.status.tostring();                                 break;                         }                     }                     else                     {                         item.subitems[1].text = _rm.getstring("checkererroroccured");                     }                 }                 catch                 {}                 lock (this)                 {                     asynccount--;                 }                                 }                         }     }      private listviewitem[] getcomps()     {         if (_frm.invokerequired)         {             getcompsdelegate d = new getcompsdelegate(getcomps);             return (listviewitem[])_frm.invoke(d);         }         else         {             listviewitem[] result=new listviewitem[_list.items.count];             _list.items.copyto(result, 0);             return result;         }     }      public void stop()     {         _thread.abort();     } } 

change background color of item in listview code this:

        if (timedout)             listview1.items[row].backcolor = color.lightcoral;         else //reset color             listview1.items[row].backcolor = listview1.backcolor; 

also since checker running on different thread main form, you'll have put code in separate function , call invoke.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -