1

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 = "";
    }
}
Andrzej
  • 33
  • 3
  • Might be easier to make the server multithreaded, the client single threaded, and run multiple client exes – Caius Jard May 22 '22 at 01:33
  • @CaiusJard wouldn't that be even worse? I wouldn't be able to receive messages on client until I provide some console input. My main objective here was to make some sort of a graceful shutdown and in the end I achieved that by calling Task.WaitAny() instead of previoud Task.WaitAll(). The other threads were ReadingThread() and ServerStatusThread() and I did get what I want - the program completes as it should. But that's a workaround. – Andrzej May 27 '22 at 15:37

0 Answers0