C# Getting return value from Elapsed Time Event -


hi have similar problem old thread still can't work around (pass return value through eventhandler) . trying implement timer calculate velocity @ intervals require elapsed time event return kind of value. i've tried using global variables event doesn't seem change variable. advice? in advance!

namespace timer_label { public static class globalvariables {     public static int _stringholder;      public static int stringholder     {         { return _stringholder; }         set { _stringholder = value; }     } }      public partial class form1 : form    {     public form1()     {         initializecomponent();          system.timers.timer mytimer = new system.timers.timer();         mytimer.elapsed += new elapsedeventhandler(displaytimeevent);         mytimer.interval = 500;         mytimer.start();           messagebox.show(convert.tostring(globalvariables.stringholder));     }    public static void displaytimeevent(object sender, elapsedeventargs e)     {            globalvariables.stringholder = "1";                                      } 

it's highly improbable timer have expired time code reaches messagebox.show statement.

try placing thread.sleep(1000) call between mytimer.start() , messagebox.show(). should give timer time elapse , execute displaytimeevent handler.

eventually, need take account fact system.timer.timer class executes callback in background thread, changes make in displaytimeevent need synchronized.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -