【问题标题】:Properly exiting a .net console application that uses .NET 4.0 Tasks正确退出使用 .NET 4.0 任务的 .net 控制台应用程序
【发布时间】:2011-05-07 17:43:02
【问题描述】:

我有一个基本上看起来像这样的控制台应用程序

class Program
{
    static void Main(string[] args)
    {
        DoStuffService svc = new DoStuffService();
        svc.Start();
    }
}


class DoStuffService
{
    public void Start()
    {
        Task.Factory.StartNew(() => { LongRunningOperation() });
    }

    private void LongRunningOperation()
    {
        // stuff
    }    
}

这些天来确保我的控制台应用程序在LongRunningOperation() 完成之前不会退出的最佳方法是什么,并且还允许我在控制台应用程序中得到通知(例如用于记录目的)LongRunningOperation() 是完成。

【问题讨论】:

    标签: c# .net multithreading c#-4.0 task-parallel-library


    【解决方案1】:

    在任务上致电Wait()。例如:

    class Program
    {
        static void Main(string[] args)
        {
            DoStuffService svc = new DoStuffService();
            svc.Start();
            // stuff...
            svc.DelayTilDone();
        }
    }
    
    
    public class DoStuffService
    {
        Task _t;
        public void Start()
        {
            _t = Task.Factory.StartNew(() => { LongRunningOperation(); });
        }
    
        public void DelayTilDone()
        {
            if (_t==null) return;
            _t.Wait();
        }
    
        private void LongRunningOperation()
        {
            System.Threading.Thread.Sleep(6000);
            System.Console.WriteLine("LRO done");
        }
    }
    

    【讨论】:

    • 而不是添加DelayTilDone()Start() 是否可以合理地返回 Program 可以用来启动 Wait( )?
    • @qntmfred:Task 不会公开 WaitHandle,因此您必须返回 Task 对象。更复杂的方法是让 Start 调用 .ContinueWith 和一个方法,该方法指示您提供的句柄,您可以等待。
    • 返回 Task 对象是有意义的,但在这一点上,我想知道 DoStuffService 类的用途。如果暴露了它使用的Task,为什么不直接使用调用者的TaskFactory呢?
    • 我希望 DoStuffService 在一个新线程上完成它的工作,无论它是用于控制台应用程序、WPF 应用程序还是 asp.net 应用程序等。这就是 DoStuffService 使用 TaskFactory 的原因。我想知道该线程何时在控制台应用程序中完成,但我不在乎它何时在其他应用程序中完成
    【解决方案2】:

    除了 Cheeso 的回答之外,您还需要处理 Console.CancelKeyPress,以便显示忙碌消息并设置 e.Cancel = True

    你无法阻止它们杀死进程,但你至少可以处理 Ctrl+C 和 Ctrl+Break。

    【讨论】:

      【解决方案3】:

      有一个类似的帖子C# multi-threaded console application - Console quits before threads complete

      您可以简单地返回一个已启动的任务和Wait()ContinueWith()

      using System.Diagnostics;
      using System.Threading.Tasks;
      
      class Program
      {
      
        static void Main(string[] args)
        {
          DoStuffService svc = new DoStuffService();
          svc.Start().Wait();//bool res = svc.Start() 
          Trace.WriteLine("333333333333333");
        }
      }
      public class DoStuffService
      {
        public Task Start()
        {
          return Task.Factory.StartNew
            (() =>
            {
              Trace.WriteLine("111111111");
              LongRunningOperation(); ;
            });
        }
        private void LongRunningOperation()
        {
          System.Threading.Thread.Sleep(3000);
          Trace.WriteLine("2222222222");
        }
      }
      

      如果要访问其 Result 属性,任务将阻塞父线程直到完成,所以:

      using System.Diagnostics;
      using System.Threading.Tasks;
      
      class Program
      {
         static void Main(string[] args)
         {
           DoStuffService svc = new DoStuffService();
           svc.Start();//bool res = svc.Start() 
           Trace.WriteLine("333333333333333");
         }
      }
      public class DoStuffService
      {
         public Task<bool> MyTask;
         public bool Start()
         {
            MyTask = Task.Factory.StartNew<bool>
              (() =>
              {
                Trace.WriteLine("111111111");
      
                return LongRunningOperation();;
              });
            return MyTask.Result;
          }
          private bool LongRunningOperation()
          {
            System.Threading.Thread.Sleep(3000);
            Trace.WriteLine("2222222222");
            return true;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2010-11-10
        • 2017-04-10
        • 1970-01-01
        • 2016-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多