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