Main Site Documentation

Using the DispatcherTimer in Glide


#1

Dear all

Unfortunately, I’m not able to use the DispatcherTimer to call a method periodicly in the UI.


// Updates the time every second
DispatcherTimer timer = new DispatcherTimer();
timer.Tick += new EventHandler(OnTimer);
timer.Interval = new TimeSpan(0, 0, 1);
timer.Start();

This example is from the GHIGraphical Taskbar demo. I’d like to use the same functions to update a time text in the LCD.

If anybody has any idea or solution, please share it with us :wink:

Regards J.


#2

Whats the error? Show some more code.


#3

No error occurs, the timer doesn’t call the OnTimer.


 public static void Main()
        {
            Glide.FitToScreen = true;
            // Tell Glide to use our custom keyboard.          
            Glide.Keyboard = InitKeyboard();               // Load the window      
            window = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.Window));
            // Activate touch       
            GlideTouch.Initialize();
            // Initialize the window.       

            // Updates the time every second
            DispatcherTimer timer = new DispatcherTimer();
            timer.Tick += new EventHandler(OnTimer);
            timer.Interval = new TimeSpan(0, 0, 1);
            timer.Start();

            Glide.MainWindow = window;
            Thread.Sleep(-1);
        }

        static void OnTimer(object sender, EventArgs e)
        {
        }



#4
timer.Interval = new TimeSpan(0, 0, 1);

You know this means the timer will fire every minute?

To fire every second:

timer.Interval = new TimeSpan(0, 0, 0, 1, 0);

#5

Both of your code snippets fires every second.


public TimeSpan(int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);


#6

Can you try and replace Thread.Sleep( -1 ) with


while (true)
    Dispatcher.Run();

If that works, then it means there is no Dispatcher running in your application (you have no Application object, f.e. in a Console Application)

See http://www.tinyclr.com/forum/1/3953/#/1/msg37313


#7

Use a regular Timer. Gilde doesn’t use the Dispatcher to make things more optimized.