コード例 #1
0
        public void Start()
        {
            if (_BaseServer.Start())
            {
                _IsStart = true;
                Console.Write("服务器启动成功!\r\n");
            }
            else
            {
                Console.Write("服务器启动失败!\r\n");
                return;
            }



            Task.Run(() => {
                _GameWorld.Start();

                //_newMail += FaxMsg;

                //Sender ss = new Sender();
                //NewMailEventArgs e = new NewMailEventArgs("123", "321", "事件测试");

                //_newMail(ss, e);

                while (_IsStart)
                {
                    for (int i = 0; i < 100; i++)
                    {
                        IWorkItem item = null;

                        if (_WorkItems.TryTake(out item))
                        {
                            item.DoWork(_GameWorld);
                        }
                    }

                    //IWorkItem item = _WorkItems.Take();

                    //item.DoWork(_GameWorld);
                }
            });
        }
コード例 #2
0
        private void DoWork()
        {
            try
            {
                OnWorkerThreadStarted();

                while (!m_ShouldStop)
                {
                    if (m_WorkerThreadManager.ShouldExitWorkerThread(this, false))
                    {
                        OnWorkerThreadExiting(WorkerThreadExitReason.MaximumThreadCountExceeded);
                        return;
                    }

                    IWorkItem workItem = m_WorkItemQueue.Dequeue();

                    if (workItem == null)
                    {
                        if (m_WorkerThreadManager.ShouldExitWorkerThread(this, true))
                        {
                            OnWorkerThreadExiting(WorkerThreadExitReason.WorkItemQueueIsEmpty);
                            return;
                        }
                        else
                        {
                            if (m_StopWhenWorkItemQueueIsEmpty)
                            {
                                OnWorkerThreadExiting(WorkerThreadExitReason.StopWhenWorkItemQueueIsEmptyIsTrue);
                                return;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }

                    lock (m_SyncLock)
                    {
                        m_CurrentWorkItem = workItem;
                    }

                    try
                    {
                        OnWorkerThreadWorkItemStarting(new WorkerThreadWorkItemStartingEventArgs(workItem));

                        try
                        {
                            workItem.DoWork();
                        }
                        catch (Exception ex)
                        {
                            workItem.State         = WorkItemState.CompletedWithException;
                            workItem.LastException = ex;

                            OnWorkerThreadWorkItemException(new WorkerThreadWorkItemExceptionEventArgs(workItem, ex));
                        }

                        if (workItem.State == WorkItemState.CompletedWithException)
                        {
                            OnWorkerThreadWorkItemException(new WorkerThreadWorkItemExceptionEventArgs(workItem, workItem.LastException));
                        }
                    }
                    finally
                    {
                        lock (m_SyncLock)
                        {
                            m_CurrentWorkItem = null;

                            OnWorkerThreadWorkItemFinished(new WorkerThreadWorkItemFinishedEventArgs(workItem));
                        }
                    }
                }

                OnWorkerThreadExiting(WorkerThreadExitReason.StopMethodIsCalled);
            }
            catch (ThreadAbortException)
            {
                Stop();

                // ThreadAbortException'ın tekrar fırlatılmamasını sağlar.
                Thread.ResetAbort();

                OnWorkerThreadExiting(WorkerThreadExitReason.ThreadAborted);
            }
            catch (Exception ex)
            {
                OnWorkerThreadException(new WorkerThreadExceptionEventArgs(ex));

                OnWorkerThreadExiting(WorkerThreadExitReason.ExceptionOccurred);
            }
        }
コード例 #3
0
 private void ProcessWorkItem(IWorkItem workItem)
 {
     Console.WriteLine("Processing work item {0}", workItem.Description);
     workItem.DoWork();
 }