Ordered Threads

I have a console application that interacts with another user interface. The interface sends commands, and my application needs to process them. It is important to continue listening to the console application while processing and executing commands in an orderly manner. Therefore, I listen to the interface on the main thread and execute commands in another thread.

Below is an example of what I'm trying, and the problem is that the threads are not ordered.

Secondly, I use locking in the ProcessCommand method, but I'm not sure if it is safe or not. For example, another thread can be inside the ProcessCommand method, so one of the processes is blocked, so the other thread can change the input value. I'm right? I did some tests, and this never happened, but was still suspicious of this.

What could be the best solution? Solution May thread or not thread.

class Program
{
    static object locker = new object();
    static void Main(string[] args)
    {
        while (true)
        {
            var input = Console.ReadLine();
            (new Thread(new ParameterizedThreadStart(ProcessCommand))).Start(input);
        }
        Console.ReadKey();
    }
    static void ProcessCommand(dynamic input)
    {
        lock (locker)
        {
            Console.WriteLine(input);
            //process incoming command
        }
    }
}
+1
source share
1 answer

You start a new stream for each line of input. That is why you cannot guarantee an order.

, "" , "" . . /, .

.NET 4 ( , dynamic), Parallel Extensions , BlockingCollection<T> IProducerConsumerCollection<T>. - Parallel Extensions # 4 -. , . BlockingCollection<T> part, .

+4

All Articles