Ejemplo n.º 1
0
 public void Update()
 {
     if (_running)
     {
         if (_reset)
         {
             CurrentTime = _time;
             _reset      = false;
         }
         CurrentTime -= _needle.Delta;
         while (CurrentTime <= 0)
         {
             OnDone.Run();
             if (Repeat)
             {
                 CurrentTime += _time;
             }
             else
             {
                 Run         = false;
                 CurrentTime = _time;
                 break;
             }
         }
     }
     else
     {
         _updater.Stop();
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Main thread method.
        /// </summary>
        public void Run()
        {
            _running = true;
            Interlocked.Increment(ref _handleCount);

            while (_running)
            {
                //#if !DEBUG
                try {
                    //#endif

                    // main thread loop
                    while (_running)
                    {
                        // iterate needles and get the next task
                        _nextIndex = Needles.Count;
                        while (--_nextIndex >= 0)
                        {
                            // is there a waiting task?
                            if (Needles[_nextIndex].Next(out _next))
                            {
                                // yes, has the context been changed?
                                if (_index != _nextIndex)
                                {
                                    // yes, update
                                    _index = _nextIndex;
                                    // set the synchronization context to allow async operations to be joined
                                    // back to the same threads assigned to the needle
                                    SynchronizationContext.SetSynchronizationContext(Needles[_index].Context);
                                }

                                //Log.D("Running : "+Needles[_nextIndex].Name+" === " + _next);

                                // try update the thread-local time values
                                Time.Update();

                                // run the task
                                _next.Run();

                                _iteration = 0;
                                _wait      = false;

                                break;
                            }
                        }

                        if (_wait)
                        {
                            // determine the thread action based on the current iteration count
                            ++_iteration;
                            Time.Update();
                            if (_iteration % 5 == 0)
                            {
                                Thread.Sleep(1);
                            }
                            else if (_iteration > HandleCount)
                            {
                                _iteration = 0;
                                Thread.Sleep(HandleCount);
                            }
                            else
                            {
                                Thread.Sleep(0);
                            }
                        }
                        else
                        {
                            _wait = true;
                        }
                    }

                    //#if !DEBUG
                } catch (Exception ex) {
                    Log.Error("Unhandled exception", ex);
                    if (!_wait)
                    {
                        _next.Stop();
                        _wait = true;
                    }
                    continue;
                }
                //#endif
            }

            // remove from the handles collection
            Handles.TakeItem().Remove(this);
            Handles.Release();

            // remove this from the handles map
            _handleMap.TakeItem().Remove(Id);
            _handleMap.Release();

            // clear this handles needle collection
            Needles.Dispose();
        }