I am working on a windows application which is developed i开发者_如何学编程n C#. I am kinda new in windows world. I want to restrict number of parallel threads running for this application at a given point of time. This number can be changed by user on a beefier machine. What is the best way to do this configuration and how it can be achieved. I can think of following approaches. 1. Use ConfigurationManager to read the configuration value. If user exists more than N number of threads show a message and give option there to modify the value of N. 2. Use registery entry?
Thanks in Advance! Sam
As Andrew says in his comment, I'm not sure if that's a good thing to do.
However, regarding the actual question. Assuming that it's a per user setting I'd have it as a user setting in the config file, that way you've got a default value, but each user can change it if they want. See here for details.
As an answer to the original question: In my opinion the best way to store configuration/settings for Windows application in .NET is to use Settings tab of project's properties: Project's Settings tab http://img6.imageshack.us/img6/8156/settingswn.png
That way, you will have a strong typed instance representing that settings.
E.g. Properties.Settings.Default.MaxWorkerThreads
Also it will provide default values for that setting, so if user haven't copied/created a config file the setting will have its default value.
As for restricting number of parallel threads as a task itself, I would recommend Task Parallel Library (TPL) or Parallel LINQ (PLINQ) (depending on the actual problem you are solving) -- especially if your application depends on the computation power only (but not on network's bandwidth, for instance). TPL have some logic to allocate an optimal number of threads when running.
Please see Parallel Programming in the .NET Framework for more topics on the subject.
精彩评论