c# - Win form app freezing upon launch -


let me first confess green programmer in dire straits trying figure out wrong application.

the goal far make timer kick off when button clicked , elapsed time continually display on text box.

there better ways implement humor me second , practice creating events , using them in programs.

what see happening when launch code freezes , never recovers, need end app task manager.

any pointers on may doing wrong , how fix appreciated.

// see clock class below containing delegate , event instantiation  public class clock {     public delegate void timechangedhandler(object clock, timeeventargs timeinfo);     public timechangedhandler timechanged;      public void runclock()     {         timeeventargs e = new timeeventargs();//initialize args         while (e.keepcounting)         {             thread.sleep(1000);             e.endtime = datetime.now;              if (e.starttime != e.endtime)             {                 e.duration = e.endtime.subtract(e.starttime);             }              if (timechanged != null)             {                 timechanged(this, e);             }           }     }   //see timeevent args description below:  public class timeeventargs : eventargs {     public timespan duration;     public datetime starttime { get; set; }     public datetime endtime { get; set; }     public bool keepcounting = false;      public timeeventargs()     {         starttime = datetime.now;         endtime = datetime.now;         keepcounting = true;     } }   //see form class below:    public partial class timeapp : form {       public timeapp()     {         initializecomponent();      }      private void startstopbutton_click(object sender, eventargs e)     {         var theclock = new clock();         var timeapp = new timeapp();         timeapp.subscribe(theclock);         theclock.runclock();      }      public void subscribe(clock theclock)     {         theclock.timechanged += new clock.timechangedhandler(newtime);     }      public void newtime(object theclock, timeeventargs e)     {          displaybox.text = e.duration.hours.tostring() + ":"             + e.duration.minutes.tostring() + ":" + e.duration.seconds.tostring();     }   } 

your runclock method blocks ui (because of thread.sleep(1000); call), makes impossible stop.

instead of looping, should @ adding windows.forms.timer form, , using drive clock.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -