I have a Visual Studio add-in that uses the System.Timers.Timer
myTimer.
Every N seconds myTimer fires and executes this code:
foreach(Window window in DTE2.Windows)
{
TextDocument td = window.Document.Object("TextDocument") as TextDocument;
// do stuff with td...
}
Because this gets called from another thread I sometimes get one of these errors:
QI for IEnumVARIANT failed on the unmanaged server.
at EnvDTE.Windows.GetEnumerator()
on line foreach(Window window in DTE2.Windows)The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))
at EnvDTE.Window.get_Document()
on line TextDocument td = window.Document.Object("TextDocument") as TextDocument;
What's the proper way to access this enumerator in another thread since COM objects are involved?
Some kind of COM thread marshalling?
Something else?