Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am writing a broker service which has two parts one which takes request from client applications and put it in a queue and there are multiple worker processes which checks this queue and if there is any work to do it will pick that piece of work and start processing. I have implemented this worker as a console application in C#. To have multiple workers I am running multiple instances of this console application.

1. Is there a better alternative to using multiple instances of console application as worker process ?
2. Can this be written as windows service ? How do I go about creating multiple workers in one service ?

Any other better way to do this ?
Updated 6-Mar-15 19:29pm
PIEBALDconsult 7-Mar-15 1:38am    
Yes. And yes.
System.Threading for a start, but there are other options.

Threading is available in all forms of C# application - and the simplest version of a thread is probably the BackgroundWorker class[^]

Have a look here: BackgroundWorker Class Sample for Beginners[^]
And here: Thread vs. BackgroundWorker[^]
Share this answer
Your approach sounds awfully bad. Multiple processes means considerable overhead not justified by anything; you really should prefer threads. Also, I'm not sure you should have many separate threads; threads are also should be reused, but I know too little about your goals and required operations to advice on further detail.

Why console application is bad? Because of… console. Why would you need it? It's not a problem: you can create a non-console application, without any windows at all. But do you need the process to execute when the user logs off? If the answer is "yes", you need to develop a Windows Service.

See also my past answer on development of dual-mode applications, behaving as interactive application or as the service, depending on how you host it: Configure Windows Service with UI[^].

Why doing so? Debugging! Debugging of the service is much harder, so you can move most of the debugging in interactive mode. How about this idea?

See also my past answers:
Configure Windows Service with UI[^],
Windows services Error[^],[^],
How to install windows service programmatically[^],
How to start the application before user login to the System[^],
Automatically start my c# application when i start my computer[^].

Share this answer
virang_21 7-Mar-15 3:43am    
Thank you for your input. I am implementing proof of concept at the moment and yes I created console app to start with so I can see all the interaction between systems. Eventually I need to move it to windows service or something else. I changed it to single application with multiple threads and each thread is constantly running and checking queue for work.
Sergey Alexandrovich Kryukov 7-Mar-15 8:34am    
You are very welcome.
Of course, there is nothing wrong with such proof-of-concept applications. I did not mean just that. What you say about your thread operations sounds reasonable.

Good luck, call again.
virang_21 15-Mar-15 19:51pm    
Hi SA, Can you please help me with below question ? This is related to this very application I am writing.
Sergey Alexandrovich Kryukov 15-Mar-15 20:07pm    
Sorry, I'm afraid not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900