Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm no completely sure what the question is as you mention a windows service and an Enter key ending your app, then you mention a better implementation.</p> <p>Firstly if you are running it in a console and don't want the enter key to kill it you can do something like</p> <pre><code>do { } while (!Console.ReadLine().Equals("exit", StringComparison.InvariantCultureIgnoreCase) ); </code></pre> <p>Instead of</p> <pre><code>Console.ReadLine(); </code></pre> <p>This means your console will only end when you type in exit and press enter, or if you close the program by pressing the x button or killing the process.</p> <p>As for better implementation, I'm not sure if this is the best practice but this is how I would do it, using a polled thread implementation:</p> <pre><code>class Poll : IDisposable { private TimeSpan polledSpan; WaitHandle[] handles = new WaitHandle[2]; ManualResetEvent exit = new ManualResetEvent(false); Thread thread; public Poll(int polledTime) { polledSpan = new TimeSpan(0, 0, polledTime); thread = new Thread(new ThreadStart(Start)); thread.Start(); } private void Start() { AutoResetEvent reset = new AutoResetEvent(false); handles[0] = reset; handles[1] = exit; bool run = true; while (run) { int result = WaitHandle.WaitAny(handles, (int)polledSpan.TotalMilliseconds, false); switch(result) { case WaitHandle.WaitTimeout: run = StuffToDo(); break; case 1: case 0: run = false; break; } } } private bool StuffToDo() { try { Console.WriteLine("Test"); return true; } catch { return false; } } public void Dispose() { exit.Set(); if (thread != null) { thread.Join(10000); } exit = null; handles = null; } } </code></pre> <p>and in the main method</p> <pre><code>Poll p = new Poll(1); do { } while (!Console.ReadLine().Equals("exit", StringComparison.InvariantCultureIgnoreCase)); p.Dispose(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload