Console.ReadLine
or Console.ReadKey
do not work as expected under Docker container environment.
Thus, typical way to solve this would be to properly handle Ctrl-C
key combination.
using System;
using System.Threading;
using System.Threading.Tasks;
namespace TestConsole
{
public class Prorgam
{
private static readonly AutoResetEvent _closing = new AutoResetEvent(false);
public static void Main(string[] args)
{
Task.Factory.StartNew(() => {
while (true)
{
Console.WriteLine(DateTime.Now.ToString());
Thread.Sleep(1000);
}
});
Console.CancelKeyPress += new ConsoleCancelEventHandler(OnExit);
_closing.WaitOne();
}
protected static void OnExit(object sender, ConsoleCancelEventArgs args)
{
Console.WriteLine("Exit");
_closing.Set();
}
}
}
open System
open System.Threading
open System.Threading.Tasks
let rec loop () =
Console.WriteLine (DateTime.Now.ToString())
Thread.Sleep (1000)
loop ()
[<EntryPoint>]
let main argv =
Task.Factory.StartNew(fun () -> loop ()) |> ignore
let closing = new AutoResetEvent(false)
let onExit = new ConsoleCancelEventHandler(fun _ args -> Console.WriteLine("Exit"); closing.Set() |> ignore)
Console.CancelKeyPress.AddHandler onExit
closing.WaitOne() |> ignore
0