I am writing a custom multithreaded TCP client-server project and I want to write from the client to server via a seperate Task which is a part of TaskFactory:
taskArray[1] = taskFactory.StartNew(() => SendingThread(tcpClient, tokenSource, cancellationToken), cancellationToken);
The Task always stops at Console.ReadLine no matter what I do, and I don't want that beacuse I want to check with each iteration if cancellationToken was cancelled by other Task. At first I simply used plain ReadLine() but the execution of the loop stops and I want to check continously if cancellationToken expired. Another of the solutions I thought would help was to check if there are any chars in buffer and only then invoke ReadLine(). However when i run the project the task allows me write only once and then never does it again. I have tried using ReadLine with timeout (source: How to add a Timeout to Console.ReadLine()?) or search in the documentation a way to clear the TextReader in Console. The effect was worse or unachievable respectively. Whole code can be found here: https://github.com/ArturMarekNowak/Networking/tree/feature/DetectionOfDisconnect/TCP. Snippet of the code:
private static void SendingThread(TcpClient tcpClient, CancellationTokenSource tokenSource, CancellationToken cancellationToken)
{
var stream = tcpClient.GetStream();
string userInput = "";
while (true)
{
// "Solution" 1:
// Stops execution
userInput = Console.ReadLine();
// "Solution" 2:
if (Console.In.Peek() > 0)
{
// Never enters again
userInput = Console.ReadLine();
}
if (string.IsNullOrEmpty(userInput))
continue;
if (userInput.Equals("q"))
tokenSource.Cancel();
if (cancellationToken.IsCancellationRequested)
return;
var bytes = Encoding.ASCII.GetBytes(userInput);
stream.Write(bytes);
userInput = "";
}
}