Ejemplo n.º 1
0
		public static Watch StartNew ()
		{
			Watch watch = new Watch ();
			watch.Start ();
          
			return watch;
		}
        static void Main(string[] args)
        {
            Watch rw = new Watch();                      

            SubscribeManager.SubscribeAll(new SubscriberNo1(rw), new SubscriberNo2(rw));            

            rw.StartWatch(1000, 1000, "repeat timer"); 
            Thread.Sleep(3000);
            
            rw.StartWatch(1000, "once time call event");
            Thread.Sleep(500);

            Thread.Sleep(5000);           

            SubscribeManager.UnsubscribeAll(); 

            Console.ReadLine();
        }
Ejemplo n.º 3
0
		public static Watch StartNew ()
		{
			Watch temp = new Watch ();
			temp.Start ();
			return temp;
		}
Ejemplo n.º 4
0
        public bool Wait(int millisecondsTimeout, CancellationToken cancellationToken)
        {
            CheckState();
            if (millisecondsTimeout < -1)
            {
                throw new ArgumentOutOfRangeException("millisecondsTimeout",
                                                      "millisecondsTimeout is a negative number other than -1");
            }

            Watch sw = Watch.StartNew();

            Func <bool> stopCondition = () => millisecondsTimeout >= 0 && sw.ElapsedMilliseconds > millisecondsTimeout;

            do
            {
                bool shouldWait;
                int  result;

                do
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    if (stopCondition())
                    {
                        return(false);
                    }

                    shouldWait = true;
                    result     = currCount;

                    if (result > 0)
                    {
                        shouldWait = false;
                    }
                    else
                    {
                        break;
                    }
                } while (Interlocked.CompareExchange(ref currCount, result - 1, result) != result);

                if (!shouldWait)
                {
                    if (result == 1)
                    {
                        handle.Reset();
                    }
                    break;
                }

                SpinWait wait = new SpinWait();

                while (Thread.VolatileRead(ref currCount) <= 0)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    if (stopCondition())
                    {
                        return(false);
                    }

                    if (wait.Count > spinCount)
                    {
                        int timeout = millisecondsTimeout < 0 ? deepSleepTime :
                                      Math.Min(Math.Max(millisecondsTimeout - (int)sw.ElapsedMilliseconds, 1), deepSleepTime);
                        handle.WaitOne(timeout);
                    }
                    else
                    {
                        wait.SpinOnce();
                    }
                }
            } while (true);

            return(true);
        }