Exemplo n.º 1
0
        private Timer CreateTimer(int sec, ushort inter)
        {
            var    countUpdated   = 0;
            var    seconds        = sec;
            ushort interval       = inter;
            var    timer          = new Timer(interval);
            var    watch          = new Stopwatch();
            var    id             = Thread.CurrentThread.ManagedThreadId;
            var    timeSpanFinish = TimeSpan.Zero;

            var list = new System.Collections.Generic.List <double>(seconds * 1000 / interval);

            timer.OnIntervalElapsed += delegate(TimeSpan timeSpan)
            {
                //UnityEngine.Debug.Log(timeSpan.TotalMilliseconds);
                countUpdated++;
                list.Add(timeSpan.TotalMilliseconds);
                timeSpanFinish += timeSpan;
                Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == id);
            };

            timer.OnTimesUp += delegate
            {
                watch.Stop();
                UnityEngine.Debug.Log(
                    "OnFinished: TotalTime " + timer.StartTime.TotalMilliseconds + " Elapsed watching seconds: " + (int)watch.Elapsed.TotalSeconds +
                    " countUpdate=" + countUpdated + " watch.Elapsed. ms" + watch.Elapsed.TotalMilliseconds + " timeSpanFinish.TotalSeconds:: " + timeSpanFinish.TotalSeconds);

                UnityEngine.Debug.Log("Min timespan:=" + list.Min() + " Max=" + list.Max() + " Average=" + list.Average());
                Assert.IsTrue((int)watch.Elapsed.TotalSeconds == seconds);
                Assert.IsTrue((int)timeSpanFinish.TotalSeconds == seconds);
                Assert.IsTrue(Thread.CurrentThread.ManagedThreadId == id);
                Assert.IsTrue(timer.RemainingTime == TimeSpan.Zero);
                Assert.IsFalse(timer.IsStarted);
                Assert.IsFalse(timer.IsPaused);
            };

            watch.Start();
            timer.Start(TimeSpan.FromSeconds(seconds));
            return(timer);
        }