Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 990997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:02:10+00:00 2026-05-16T06:02:10+00:00

I have an console application which interact another user interface. Interface sends commands and

  • 0

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
        }
    }
}
  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:02:10+00:00Added an answer on May 16, 2026 at 6:02 am

    You’re starting a new thread for each line of input. That’s why you can’t guarantee the ordering.

    Instead of that, create a queue which the “reader” can put lines into, and the “processor” can read from. Have one thread reading and one thread processing. I have some sample code in my threading tutorial for a producer/consumer queue, although it’s very simplistic.

    If you’re using .NET 4 (which I guess you are given the use of dynamic) then Parallel Extensions makes this much easier using BlockingCollection<T> and IProducerConsumerCollection<T>. Today is your lucky day – Joe Albahari has made the Parallel Extensions part of C# 4 in a Nutshell public on his web site. Read through that and it should make everything much clearer. You may want to skip to the BlockingCollection<T> part, but it’s all worth reading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a console application in which I have exposed interface having [OperationContract(IsOneWay
I have a Console Application which gets a string from another program. The string
I have a console application which I am running as a process from my
I have a console application which is intended to just keep running until it
I have a task which is to create a C# console application which should
I have a C# 4.0 Windows Console application which should run 24*7*365. (I have
I have a console application which is parsing HTML documents via the WebRequest method
I have a console application which has target .NET 2.0 It is very short
I have a console application which can also open winform under certain conditions.Processing(its a
I have a c++ console application which I would like to publish using clickonce.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.