开发者

Is there a way of restricting an API's processor resource in c#?

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

  1. Make the API call from a child process
  2. 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜