The app I am working on calls upon an external API to process certain requests the user may make. This is of course done in a seprate thread to try and prevent the UI from freezing or slowing down. However, the external API produces it's own mass of th开发者_高级运维reads and does some processor intensive work. As soon as calls are made to it, the CPU usage goes up to 100%. This then locks up my UI. Is there any way in C# of restricting the available CPU usage to a third party API?
The only thing I can think of in this scenario, is change the thread priorities. You could increase your UI thread's priority a little, or try to lower the other thread's priorities.
The latter is considered more proper, but in your case a bit tricky, as you will have to query for all running threads, find out which are the worker threads, and lower their priority. You probably need to detect new threads and alter their priority too.
I'm unaware of a way to restrict an API call to a given processor resource. There are definitely ways to curtail processor usage within an Application but not along a given API path.
I think the best solution for you here is to do the following
- Make the API call from a child process
Restrict the child process execution such that it won't peg 100% of all CPUs. The simplest way to do this is to give the process a lower priority.
- http://msdn.microsoft.com/en-us/library/system.diagnostics.process.priorityclass.aspx
精彩评论