开发者

Properly exiting a .net console application that uses .NET 4.0 Tasks

开发者 https://www.devze.com 2023-03-03 09:58 出处:网络
I have a console application that essentially looks like this class Program { static void Main(string[] args)

I have a console application that essentially looks like this

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
    }    
}

What's the best way these days to ensure my console application doesn't exit before LongRunningOperation() is complete, and also allows me a way to be notified in the console application (for logging purposes for instance) that LongRunningOperation() is complete.


call Wait() on the task. For example:

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");
    }
}


In addition to Cheeso's answer, you'll want to handle Console.CancelKeyPress so that you can display a busy message and set e.Cancel = True.

There's nothing you can do to prevent them from killing the process, but you can at least handle Ctrl+C and Ctrl+Break.


There is a similar thread C# multi-threaded console application - Console quits before threads complete

You can simply return a started task and Wait() or ContinueWith() on it:

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");
  }
}

A task will block the parent thread until completion, if to access its Result property, so:

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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消