Quit correctly .net console application using .NET 4.0 tasks

I have a console application that looks something like this:

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


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

    private void LongRunningOperation()
    {
        // stuff
    }    
}

What is the best way these days to ensure that my console application does not exit until completion LongRunningOperation(), and also allows me to notify in the console application (for example, for logging) that LongRunningOperation()is complete.

+3
source share
3 answers

call Wait()in task. For instance:

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

In addition to responding to Cheeso, you need to process Console.CancelKeyPressso that you can display the busy message and install e.Cancel = True.

, , Ctrl + C Ctrl + Break.

+1

There is a similar thread . C # multithreaded console application. - The console terminates before the threads complete.

You can simply return the initial 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");
  }
}

The task blocks the parent thread until completion if to access the Result property, therefore:

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
source

All Articles