c# – CoreAudio OnVolumeNotification事件订阅导致explorer.exe中的CPU使用率过高

前端之家收集整理的这篇文章主要介绍了c# – CoreAudio OnVolumeNotification事件订阅导致explorer.exe中的CPU使用率过高前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
背景:在 Windows Vista及更高版本中,使用扩展的 @L_301_1@(由Ray Molenkamp和Xavier Flix)通过订阅DefaultAudioEndpoint的OnVolumeNotification并在更改时设置卷来强制执行卷级别.

问题:功能上成功,但只要注册了OnVolumeNotification,cpu就会根据cpu的功率固定在30-50%.经过Process Explorer&进程监视器显示,explorer.exe和有时svchost.exe将被注册表读取调用消耗.我不确定哪个注册表项.我不相信我以有害的方式订阅此活动,因为我仔细管理订阅 – 它只被解雇一次.

强制执行卷的逻辑过程

>取消订阅端点OnVolumeNotification
>设置端点卷标量属性(立即生效)
>订阅端点OnVolumeNotification

Core Audio API中涉及的基础win32方法RegisterControlChangeNotifyUnregisterControlChangeNotify.问题是否可能是由这些或事件订阅的实现引起的?

解决方法

而不是:

>取消订阅
>更改音量/设置静音
>重新订阅

修改了我的逻辑,主要使用带有支持字段的属性中的逻辑来管理何时更新.它并不完美,但它非常接近并且它不占用任何cpu,它允许从滑块的外部输入完全支持INPC.

  1. public EndpointVolumeEnforcer() {
  2. try {
  3. mmDeviceEnumerator = new MMDeviceEnumerator();
  4. mmDevice = mmDeviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender,ERole.eMultimedia);
  5. audioEndpointVolume = mmDevice.AudioEndpointVolume;
  6. audioEndpointVolume.OnVolumeNotification += data => {
  7. VolumePercent = Convert.ToInt16(data.MasterVolume*100);
  8. DeviceIsMuted = data.Muted;
  9. };
  10. DesiredVolume = 65;
  11. }
  12. catch (Exception ex) {
  13. // Logging logic here
  14. }
  15. }
  16.  
  17. public int DesiredVolume {
  18. get { return _desiredVolume; }
  19. private set {
  20. if (_desiredVolume == value) return;
  21. _desiredVolume = value;
  22. NotifyOfPropertyChange();
  23. Enforce(_desiredVolume);
  24. }
  25. }
  26.  
  27. public int VolumePercent {
  28. get { return volumePercent; }
  29. private set {
  30. if (volumePercent == value) return;
  31. volumePercent = value;
  32. if (volumePercent != _desiredVolume) {
  33. volumePercent = _desiredVolume;
  34. Enforce(volumePercent);
  35. }
  36. }
  37. }
  38.  
  39. public void Enforce(int pct,bool mute = false) {
  40. var adjusted = Convert.ToInt16(audioEndpointVolume.MasterVolumeLevelScalar*100);
  41. if (adjusted != DesiredVolume) {
  42. audioEndpointVolume.MasterVolumeLevelScalar = pct/100f;
  43. }
  44. }

猜你在找的C#相关文章