none
How can a C# console program handle multiple requests (receives at the same moment)? RRS feed

  • Question

  • Hello,

    I wrote a c# console app that can receive console arguments than executes some code based on the received arguments from command line also it has a basic queue structure. This queue structure handles requests which are received when the current execution continues. However, my program fails when more than one requests comes at the same time and only one of them is executed.

    Is there any way that an exe can handle multiple execution requests at the same moment? In my case, one trigger app runs a command ( ex: Myapp.exe argument[i] argument[j]) and sends arguments into my program. For example, if the trigger app founds 3 matching conditions at the same time, it runs "Myapp.exe s1 message", "Myapp.exe s2 message" and "Myapp.exe s3 message" commands at the same moment. And Myapp.exe can't handle this commands.

    Best regards

    Ugur


    Saturday, December 29, 2018 7:14 PM

All replies

  • Hello,

    You will have a better chance at someone helping you by showing code that has been coded so far and when posting code use the code block button.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, December 29, 2018 8:10 PM
    Moderator
  • Thank you for your kind advice Kareninstructor. I think I don't need to share code now. Because, my question is  kind of theoretical.

    Regards.


    • Edited by uk3435 Saturday, December 29, 2018 9:11 PM
    Saturday, December 29, 2018 9:08 PM
  • Maybe the solution should use a Windows service that is hosting a WCF MSMQ service, which is always running, a multi threaded solution/multi user/client solution. Your exe program as the client sends commands to the WCF MSMQ service to be processed by the services. So, the exe program can send in multiple commands to the WCF service. 

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/queuing-in-wcf

    https://www.c-sharpcorner.com/UploadFile/17e8f6/msmq-in-wcf/

    You could also let the console program host the WCF service.

    Saturday, December 29, 2018 10:24 PM
  • Hi uk3435,

    Thank you for posting here.

    For your issue, you want to handle multiple requests in a C# console program at the same moment. So I suggest you could use Parallel.foreach implement this function.

    Here is my simple code.

    static void Main(string[] args)
    
            {
    
                Process process1 = new Process();
    
                process1.StartInfo.Arguments = " Myapp.exe s1 message";
    
                process1.StartInfo.FileName = "cmd.exe";
    
                Process process2 = new Process();
    
                process2.StartInfo.Arguments = " Myapp.exe s2 message";
    
                process2.StartInfo.FileName = "cmd.exe";
    
                Process process3 = new Process();
    
                process3.StartInfo.Arguments = " Myapp.exe s3 message";
    
                process3.StartInfo.FileName = "cmd.exe";
    
                List<Process> list = new List<Process>();
    
               list.Add(process1);
    
                list.Add(process2);
    
                list.Add(process3);
    
                Parallel.ForEach(list, (process) =>
    
                 {
    
                     process.Start();
    
                 }
    
                );
    
                Console.ReadKey();
    
            }


    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, December 31, 2018 9:51 AM
    Moderator
  • This is a bit ironic (given what Karen posted)!!!

    Jack, you should know better (being a MSFT CSG) … use the "Insert Code Block" button.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Monday, December 31, 2018 2:54 PM
    Moderator
  • Hello,

    Thank you very much for your reply.

    Best regards

    Wednesday, January 2, 2019 9:24 AM
  • Hi uk3435,

    If your question has been solved, please mark the useful reply as answer to close the thread. This will make answer searching easier in the forum and be beneficial to community members. 

    Best regards,

    Jack 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 3, 2019 7:13 AM
    Moderator