• 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;
                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!



    Tuesday, March 6, 2012 12:44 PM


All replies

  • 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.
    Tuesday, March 6, 2012 5:19 PM
  • 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?


    Tuesday, March 6, 2012 6:57 PM
  • 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.

    Tuesday, March 6, 2012 7:20 PM
  • Neil,

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


    Jeff Sanders (MSFT)

    Tuesday, March 6, 2012 8:29 PM
  • Thanks to you both,


    Friday, March 9, 2012 12:40 PM
  • My pleasure Neil, enjoy coding!!

    Jeff Sanders (MSFT)

    Friday, March 9, 2012 12:41 PM