Exemplo n.º 1
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="id"></param>
 /// <param name="handler"></param>
 public void Enqueue(string id, QEventHandler handler)
 {
     if (Thread.CurrentThread.ManagedThreadId == _WorkingThread.ManagedThreadId)
     {
         if (!_Stop)
         {
             try
             {
                 handler();
             }
             catch (Exception ex)
             {
                 EventException?.Invoke(this, new Exception("ERROR running " + id + ": " + ex.Message));
                 //throw new Exception("ERROR running " + id + ": " + ex.Message);
             }
         }
     }
     else
     {
         lock (_Queue)
         {
             if (!_Stop)
             {
                 _Queue.Enqueue(new Event(id, handler));
                 _NewEvent.Set();
             }
         }
     }
 }
Exemplo n.º 2
0
 protected void SafeRaiseEventException(Exception x)
 {
     EventException?.Invoke(this, new WebServerExceptionEventArgs()
     {
         X = x
     });
 }
Exemplo n.º 3
0
        private void ProcessEvents()
        {
            WaitHandle[] waitHandles = new WaitHandle[] { _StopEvent, _NewEvent };

            while (!_Stop)
            {
                Event ev = null;

                lock (_Queue)
                {
                    if (_Queue.Count > 0)
                    {
                        ev = _Queue.Dequeue();
                    }
                    else
                    {
                        _NewEvent.Reset();
                    }
                }

                if (ev == null)
                {
                    if (WaitHandle.WaitAny(waitHandles) == 0)
                    {
                        break;
                    }
                    else
                    {
                        lock (_Queue)
                        {
                            ev = _Queue.Dequeue();
                        }
                    }
                }

                if (ev.Valid)
                {
                    try
                    {
                        ev.Handler();
                    }
                    catch (Exception ex)
                    {
                        EventException?.Invoke(this, new Exception("ERROR running " + ev.Id + ": " + ex.Message));
                        //throw new Exception("ERROR running " + ev.Id + ": " + ex.Message);
                    }
                }
            }
        }