Hello everybody
I have an console application which interact another user interface. Interface sends commands and my application should to process them. It is important to keep listening my console application while processing and execute commands in ordered. So i listen interface on main thread and execute commands in another thread.
Below example is what i am trying and problem is execution threads are not ordered.
Second thing is i am using lock in ProcessCommand method but i am not sure it is safe or not. For example one more threads can be inside of ProcessCommand method so one was process in lock so other thread can change incoming input value. Am i right ? I did some test and that never happen but still suspicious about it.
What can be best solution ? Solution Can be threading or not threading.
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
}
}
}