我正在编写一个应用程序来修改另一个应用程序的容量,并使用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进行此操作。以下代码块在主线程上可以正常工作,但是在...