Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="work">work的创建和销毁由外界处理</param>
        /// <param name="run"></param>
        /// <returns></returns>
        public void AddWork(IWork work, bool run = false)
        {
            if (work != null && !allWorks.Contains(work))
            {
                allWorks.Add(work);

                if (run)
                {
                    if (runningCount < runningWorks.Count)
                    {
                        runningWorks[runningCount++] = work;
                    }
                    else
                    {
                        runningWorks.Add(work);
                        runningCount++;
                    }

                    work.Start();
                    (work as ITick).Tick(0);                    //有些需要立即生效
                    if (work.IsFinished())
                    {
                        allWorks.Remove(work);
                        runningWorks.Remove(work);
                        runningCount--;
                    }
                }
            }
        }
Exemple #2
0
 public void AddStartRightAwayWork(IWork work)
 {
     if (work != null && !allWorks.Contains(work))
     {
         AddWork(work);
         work.Start();
         TickOnEditorMode();
     }
 }
Exemple #3
0
 protected override void DoTick(float deltaTime)
 {
     if (!conditionSatisfied)
     {
         conditionSatisfied = condition();
         if (conditionSatisfied && then != null)
         {
             then.Start();
         }
     }
     if (conditionSatisfied && then != null)
     {
         then.Tick(deltaTime);
     }
 }
 /// <summary>
 /// Starts dedicated crawler's thread which countinously takes and executes work.
 /// </summary>
 /// <param name="workSource">Crawler worksource</param>
 public void StartCrawling(WorkSource workSource)
 {
     if (thread == null)
     {
         thread = new Thread(new ThreadStart(() =>
         {
             while (!shouldStop)
             {
                 try
                 {
                     log.Debug("Getting available work...");
                     work = workSource.GetAvailableWork();
                     if (work != null)
                     {
                         log.DebugFormat("Executing work [{0}]", work);
                         work.Start();
                         log.InfoFormat("Work finished successfully. [{0}]", work);
                         workSource.ReturnFinishedWork(work);
                         log.DebugFormat("Work returned to the source. [{0}]", work);
                     }
                 }
                 catch (Exception ex)
                 {
                     log.ErrorException("Exception occured while executing a work. You should take care for all exceptions while you implement 'ICrawlerJob.Start()' method.", ex);
                 }
             }
             log.Info("Crowler was stopped.");
         }));
         thread.Name = name;
         thread.Start();
     }
     else
     {
         log.FatalFormat("Crawler '{0}' is already running on another source.", name);
         throw new InvalidOperationException(String.Format("Crawler '{0}' is already running on another source.", name));
     }
 }