Esempio n. 1
0
        private void HandlerWork(QCEvent e, IQCEventJob handler, bool inNewThread)
        {
            try
            {
                Logger.Info("Start work,Event is :" + e.ToString() + ",job is :" + handler.ToString() + "");

                handler.Work(inNewThread);
            }

            catch (SendMessageException smse) {
                throw smse;
            }

            catch (Exception ne)
            {
                Logger.Info("The work have some error ,Event is :" + e.ToString() + ",job is :" + handler.ToString() + "");
                Logger.LogException(ne);
                Logger.Error(e.StatusString());

                Logger.Error("System Status Dumping ");
                foreach (var dSource in EventSources)
                {
                    Logger.Error(dSource.StatusString());
                }

                foreach (var dFactory in EventJobFactories)
                {
                    Logger.Error(dFactory.StatusString());
                }
                Logger.Error("System Status Dump End");

                if (inNewThread)
                {
                    // remeber to release the semaphore
                    Semaphore.Release();
                }
                throw ne;
            }
        }
Esempio n. 2
0
        protected override QCServiceWorkState Work()
        {
            bool idle = true;

            Logger.Info("Start Main service Work()!");
            foreach (var source in EventSources)
            {
                try
                {
                    if (source.HasNextEvent())
                    {
                        Logger.Info("source.HasNextEvent()");
                        QCEvent e = source.NextEvent();

                        Debug.Assert(e != null, "Event can't be null");

                        bool eventHasNotBeenAccept = true;

                        foreach (IQCEventJobFactory handlerFactory in EventJobFactories)
                        {
                            if (handlerFactory.AcceptEvent(e))
                            {
                                eventHasNotBeenAccept = false;
                                IQCEventJob handler = handlerFactory.NewJob(e);
                                Logger.Info(" Handler: " + handlerFactory.GetType() + " Event: " + source.GetType());

                                if (IsSTAThread)
                                {
                                    Logger.Info("STAThread, do without new thread");
                                    HandlerWork(e, handler, false);
                                }
                                else
                                {
                                    Semaphore.WaitOne(); // need to be the last line

                                    Task.Factory.StartNew(() =>
                                    {
                                        HandlerWork(e, handler, true);
                                    });
                                }
                            }
                        }

                        if (eventHasNotBeenAccept)
                        {
                            Logger.Debug("Event " + e + " has not been accepted by any factory. Skipped");
                        }

                        idle = false;
                    }
                }
                catch (QCEventIOException e)
                {
                    Logger.Debug("Event Fetching Error: " + e);
                }
                catch (SendMessageException smse) {
                    throw smse;
                }
            }

            if (idle)
            {
                return(QCServiceWorkState.Idle);
            }
            else
            {
                return(QCServiceWorkState.Working);
            }
        }