none
DispatcherTimer

    Domanda

  • I am trying to create a simple timer in Javascript/HTML with no joy.  I've done it using XAML and C# but I wanted to compare it with doing it with js and html and it just won't work.  I'm fairly sure I'm being daft or maybe too simplistic but I'm not sure.

    I couldn't fathom how to do it in Javascript so I wrote a C# class that uses the DispatcherTimer, it is simple enough, here is the code:

        public sealed class TimingFunctions
        {
            DispatcherTimer timer = new DispatcherTimer();
    
            public string Start()
            {
                DateTime now = DateTime.Now;
                timer.Start();
                EventHandler<Object> tick = Tick;
                timer.Tick += tick;
                timer.Interval = new TimeSpan(0, 0, 0, 0, 25);
                return "blah";
            }
    
            public event EventHandler<Object> Tick;
        }

    I call it from javascript like this:

        function startTiming() {
            var startDate = new Date();
            var startTime = startDate.getTime();
    
            var foo = new TimingLibraryMD.TimingFunctions();
            var s = foo.start();
            foo.addEventListener("tick", updateTime);
        };

    Now, I have been hacking this around for a while so there is some redundant stuff in there.  Anyway, when I run it I get:

     JavaScript runtime error: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

    I'm stumped.  Can anyone point me in the right direction?  I suspect i'm doing something dumb but I'm losing the will to live!

    Thanks,

    Neil

    martedì 6 marzo 2012 12:44

Risposte

Tutte le risposte

  • If you want to simply create a timer in JavaScript then you probably want to use setTimeout.

    window.setTimeout(updateTime, 25);

    Other than that it seems the C# DispatchTimer is calling back on the wrong thread. 


    If this is for a game loop you might want to use msRequestAnimationFrame.
    martedì 6 marzo 2012 17:19
  • Andy,

    Thanks, I'll have a look at the timeout. I want to be able to do other tasks in the app but have a field periodically updating, it isn't a game. Would that do it or would it block other tasks? I'll look into it.

    With regards the thread issue, since it will continue to bug me, am I doing something wrong for it to call back on the wrong thread? I could see it was something like that from the error but I cannot see what I should do to fix it. Any suggestions?

    Thanks

    martedì 6 marzo 2012 18:57
  • Afraid I'm not an expert on DispatchTimer or the implications it has on the threading model. On the face of it sounds like it should dispatch on the UI thread. Perhaps worth trying another kind of timer and then marshaling between threads as needed yourself.

    martedì 6 marzo 2012 19:20
  • Neil,

    You will want to use setTimeout for your Javascript app as Andy mentioned.

    -Jeff


    Jeff Sanders (MSFT)

    martedì 6 marzo 2012 20:29
    Moderatore
  • Thanks to you both,

    Neil

    venerdì 9 marzo 2012 12:40
  • My pleasure Neil, enjoy coding!!

    Jeff Sanders (MSFT)

    venerdì 9 marzo 2012 12:41
    Moderatore