all groups > dotnet interop > october 2006 >
You're in the

dotnet interop

group:

How to Interop between .NET (VSTO) and COM



How to Interop between .NET (VSTO) and COM Tk_Neo
10/5/2006 7:29:02 AM
dotnet interop: Hi,

I will explain my problem in brief. I am creating a VSTO app. There is more
than one thread in the app that tries to write to the Excel COM object i
have. Now Office COM is STA and can handle one thread at a time. So if the
user is already editing some cell, my background worker thread trying to
write on Excel will raise an exception.

Now to be able to handle this gracefull i need to implement the
IMessageFilter Intrerface (COM version) and implement the RetryRejectedCall
method so that i can retry. I also need to make a win32 call to the
CoRegisterMessageFilter method to register my interface.

Now i need a sample code for this as there is NO CODE anywhere where someone
has implemented the COM IMessageFilter Interface.

Can please someone help me out. There is no other way out for me.
RE: How to Interop between .NET (VSTO) and COM Mostafa Elhemali
10/17/2006 3:39:02 PM
Hi,
OK I'm actually faced with a pretty similar problem to you - except I'm
trying to automate Visual Studio not Office. Since I don't know how to
implement this interface, my work-around current is to implement a method
ExecWithRetry() that retries any method call for a set amount of time, then
have each call passed into ExecWithRetry() instead of called alone. My code:

private delegate void ComTask();

private void ExecWithRetry(ComTask task)
{
DateTime startTime = DateTime.Now;
bool done = false;
while (!done && DateTime.Now.Subtract(startTime).Seconds < 120)
{
try
{
task();
done = true;
}
catch (System.Runtime.InteropServices.COMException ex)
{
if ((uint)ex.ErrorCode == 0x80010001)
{
// RPC_E_CALL_REJECTED - sleep for half a second then try again
System.Threading.Thread.Sleep(500);
}
}
}
}
// Sample call
ExecWithRetry(delegate { dte8Obj.MainWindow.Activate(); });

[quoted text, click to view]
AddThis Social Bookmark Button