multithreading - C# Console Application - How do I always read input from the console? -
i writing console application uses lot of multithreading. want able allow user enter console however, there regular output console threads want users able enter things console , me handle input.
how achieve this? i've found nothing online it?
thanks in advanced!
this c# btw!
class program { static void main(string[] args) { // cancellation keyboard string cancellationtokensource cts = new cancellationtokensource(); // thread listens keyboard input var kbtask = task.run(() => { while(true) { string userinput = console.readline(); if(userinput == "c") { cts.cancel(); break; } else { // handle input console.writeline("executing user command {0}...", userinput); } } }); // thread performs main work task.run(() => dowork(), cts.token); console.writeline("type commands followed 'enter'"); console.writeline("enter 'c' end program."); console.writeline(); // keep console running until cancellation token invoked kbtask.wait(); } static void dowork() { while(true) { thread.sleep(3000); console.writeline("doing work..."); } } }
Comments
Post a Comment