2008-12-29 11 views
1

Je crée une application en C# qui a une classe statique qui initie une classe COM et gère certains gestionnaires d'événements d'une autre classe qui utilise le clavier. Lorsque j'appelle une méthode de la classe COM à partir d'un gestionnaire d'événement de bouton dans ma fenêtre WPF, la méthode fonctionne sans problème mais lorsque je l'appelle dans l'un des callbacks de ma classe statique, elle renvoie l'exception suivante:InvalidCastException: RPC_E_CANTCALLOUT_ININPUTSYNCCALL

Unable to cast COM object of type 'BLAHBLAH' to interface type 'BLAHBLAH'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{9DD6680B-3EDC-40DB-A771-E6FE4832E34A}' failed due to the following error: An outgoing call cannot be made since the application is dispatching an input-synchronous call. (Exception from HRESULT: 0x8001010D (RPC_E_CANTCALLOUT_ININPUTSYNCCALL)).

Pouvez-vous s'il vous plaît me dire, ce que cette exception signifie et comment puis-je le résoudre?

Répondre

1

Enveloppez votre code dans un nouveau thread:

Thread thread = new Thread(() => 
{ 
    ManagementObjectSearcher theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive"); 
    foreach (ManagementObject currentObject in theSearcher.Get()) 
    { 
     Debug.WriteLine("Device present: " + currentObject);   
     ManagementObject theSerialNumberObjectQuery = new ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'"); 
     serial = theSerialNumberObjectQuery["SerialNumber"].ToString(); 
    } 
}); 
thread.Start(); 
thread.Join(); //wait for the thread to finish