带有线程的CoreAudioApi-InvalidCastException

问题描述 投票:2回答:2

我正在编写一个应用程序来修改另一个应用程序的容量,并使用CoreAudioApi dll来执行此操作。以下代码块可在主线程上完美运行,但是在单独的线程中调用时,会引发InvalidCastException。我抛出了Mutex,以防万一这只是两个线程试图访问相同资源的问题,但事实并非如此。关于可能是什么问题的任何想法?我陷入了一个新的C#程序员的困境。我读了其他问题,它提出只有主线程才能访问COM对象(例如CoreAudioApi),因此我需要向主线程发送某种消息。如果正确的话,什么是最好的方法?

异常(在第一个Math.abs if语句上发生:]]

Unable to cast COM object of type 'System.__ComObject' to interface type 'CoreAudioApi.Interfaces.IMMDevice'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{D666063F-1587-4E43-81F1-B948E807363F}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

代码:

muteSoundMutex.WaitOne();
AudioSessionControl sASC = sInfo.getSAudioSession();
if (Math.Abs(sASC.SimpleAudioVolume.MasterVolume -
      (0.05f / defaultDevice.AudioEndpointVolume.MasterVolumeLevelScalar)) < 0.0001 
    && savedVol > 0)
{
   ... // other code here. all mutexes are released correctly.
    

我正在编写一个应用程序来修改另一个应用程序的音量,并使用CoreAudioApi dll进行此操作。以下代码块在主线程上可以正常工作,但是在...

c# multithreading com .net
2个回答
2
投票

您的错误消息告诉您在使用IMMDevice进行某些操作期间的转换错误。根据this规范,IMMDevice只有4种方法:


0
投票

我的应用抛出此异常。我已经对此收集了一些见识。

© www.soinside.com 2019 - 2024. All rights reserved.