Building C# console app for multiple instances

这一生的挚爱 提交于 2019-12-11 06:46:22

问题


I'm building a console application which imports data into databases. This is to run every hour depending on an input CSV file being present. The application also needs to be reused for other database imports on the same server, e.g. there could be up to 20 instances of the same .exe file with each instance having their own separate configuration.

At the moment I have the base application which passes a location of config file via args, so it can be tweaked depending on which application needs to use it. It also undertakes the import via a transaction, which all works fine.

I'm concerned that having 20 instances of the same .exe file running on the same box, every hour, may cause the CPU to max out?

What can I do to resolve this? Would threading help?


回答1:


Why not make a single instance that can handle multiple configurations? Seems a lot easier to maintain and control.




回答2:


Each executable will be running in it's own process, and therefore, with it's own thread(s). Depending on how processor intensive each task is, the CPU may well max out but this is not necessarily something to be concerned about. If you are concerned about concurrent load then the best way may be to stagger the scheduling of your processes so that you have the minimum number of them running simultaneously.




回答3:


No, this isn't a threading issue.

Just create a system-wide named Mutex at the start of the application. When creating that Mutex, see if it already exists. If it does, it means that there is another instance of your application running. At this point you can give the user a message (via the console or message box) to say that another instance is already running, then you can terminate the application.




回答4:


I realize this thread is very old but I had the very same issues on my project. I suggest using MSMQ to process jobs in sequence.



来源:https://stackoverflow.com/questions/4062661/building-c-sharp-console-app-for-multiple-instances

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!