public static void Run()
        {
            // 初始化具体服务类
            var concreteService = new ConcreteBackgroundTaskService();

            concreteService.ThreadStatusChanged += (e) =>
            {
                switch (e.Status)
                {
                case ServiceThreadStatus.Initialized:
                    Logger.Trace("  Thread-{0} GetTaskAsync.", Thread.CurrentThread.ManagedThreadId);
                    break;

                case ServiceThreadStatus.Completed when e.Task != null:
                    Logger.Trace("  Thread-{0} ExecuteTaskAsync(TaskId={1}) completed.",
                                 Thread.CurrentThread.ManagedThreadId, e.Task.Id);
                    break;

                default:
                    Logger.Trace("  Thread-{0} {1}.", Thread.CurrentThread.ManagedThreadId, e.Status.ToString());
                    break;
                }
            };

            // 开始启动服务。
            // 如果在 Windows 服务中可以在 OnStart 事件里面调用
            concreteService.Start();

            while (true)
            {
                ConsoleKeyInfo keyInfo = Console.ReadKey(false);
                if (keyInfo.Key == ConsoleKey.Escape)
                {
                    // 停止服务。如果在 Windows 服务中可以在 OnStop 事件里面调用
                    concreteService.Stop();
                    concreteService.Dispose();
                    break;
                }

                if (keyInfo.Key == ConsoleKey.A)
                {
                    Console.WriteLine("Input the task num:");
                    string taskNumStr = Console.ReadLine();
                    int.TryParse(taskNumStr, out int taskNum);
                    if (taskNum > 0)
                    {
                        taskNum = ConcreteData.AddTasks(taskNum);
                    }
                    Console.WriteLine("{0}  Add new tasks:{1}",
                                      DateTime.Now.ToLongTimeString(), taskNum.ToString());
                }
            }

            Console.WriteLine("Press any key to exit.");
            Console.Read();
        }
    public static void Run()
    {
      // 初始化具体服务类
      var concreteService = new ConcreteTaskThreadService();
      concreteService.ThreadStarted += ConreteService_ThreadStarted;
      concreteService.ThreadExited += ConreteService_ThreadExited;
      concreteService.TaskExecuting += ConreteService_TaskExecuting;
      concreteService.TaskExecuted += ConreteService_TaskExecuted;
      concreteService.ServiceStoped += ConreteService_ServiceStoped;

      // 开始启动服务。
      // 如果在Windows服务中可以在OnStart事件里面调用
      concreteService.Start();

      Logger.Trace("{0} Started.  AllowedThreadMax={1}, TaskIdleTime={2}ms, TaskBusyTime={3}ms.",
          concreteService.Name,
          concreteService.AllowedThreadMax.ToString(),
          concreteService.TaskIdleTime.TotalMilliseconds.ToString(),
          concreteService.TaskBusyTime.TotalMilliseconds.ToString());

      while (true)
      {
        ConsoleKeyInfo keyInfo = Console.ReadKey(false);
        if (keyInfo.Key == ConsoleKey.Escape)
        {
          // 停止服务。如果在Windows服务中可以在OnStop事件里面调用
          concreteService.Stop();
          concreteService.Dispose();
          break;
        }

        if (keyInfo.Key == ConsoleKey.A)
        {
          Logger.Trace("Input the task num:");
          string taskNumStr = Console.ReadLine();
          int.TryParse(taskNumStr, out int taskNum);
          if (taskNum > 0)
            taskNum = ConcreteData.AddTasks(taskNum);
          Logger.Trace("Add new {1} tasks.", taskNum.ToString());
        }
      }

      Console.WriteLine("Press any key to exit.");
      Console.Read();
    }