【发布时间】:2014-07-10 23:54:27
【问题描述】:
我有一个用 C#(使用 .NET Framework 3.5)编写的进程内 COM 服务器,它基于此示例引发 COM 事件: http://msdn.microsoft.com/en-us/library/dd8bf0x3(v=vs.90).aspx
Excel VBA 是我的 COM 服务器最常用的客户端。我发现当我在 Excel 处于编辑模式时引发 COM 事件(例如,正在编辑单元格)时,该事件“丢失”。这意味着,永远不会调用 VBA 事件处理程序(即使在 Excel 编辑模式完成后),对 C# 事件委托的调用也会通过并静默失败,不会引发任何异常。有谁知道我如何在我的 COM 服务器上检测到这种情况?还是最好在 Excel 退出编辑模式之前阻止事件委托调用?
我试过了:
- 检查事件委托的属性 - 找不到任何属性表明事件未能在客户端引发。
- 直接从工作线程和主线程调用事件委托 - 客户端未引发事件,服务器未引发异常。
- 将事件委托推送到工作线程的 Dispatcher 并同步调用 - 客户端不会引发事件,服务器不会引发异常。
- 将事件委托推送到主线程的 Dispatcher 并同步和异步调用它 - 客户端不会引发事件,服务器不会引发异常。
- 检查 Dispatcher.BeginInvoke 调用的状态代码(使用 DispatcherOperation.Status) - 状态始终以“Completed”结束,并且永远不会处于“Aborted”状态。
- 创建进程外 C# COM 服务器 exe 并测试从那里引发事件 - 结果相同,从未调用事件处理程序,没有异常。
由于没有迹象表明该事件未在客户端引发,因此我无法在我的代码中处理这种情况。
这是一个简单的测试用例。 C# COM 服务器:
namespace ComServerTest
{
public delegate void EventOneDelegate();
// Interface
[Guid("2B2C1A74-248D-48B0-ACB0-3EE94223BDD3"), Description("ManagerClass interface")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
public interface IManagerClass
{
[DispId(1), Description("Describes MethodAAA")]
String MethodAAA(String strValue);
[DispId(2), Description("Start thread work")]
String StartThreadWork(String strIn);
[DispId(3), Description("Stop thread work")]
String StopThreadWork(String strIn);
}
[Guid("596AEB63-33C1-4CFD-8C9F-5BEF17D4C7AC"), Description("Manager events interface")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
[ComVisible(true)]
public interface ManagerEvents
{
[DispId(1), Description("Event one")]
void EventOne();
}
[Guid("4D0A42CB-A950-4422-A8F0-3A714EBA3EC7"), Description("ManagerClass implementation")]
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(ManagerEvents))]
public class ManagerClass : IManagerClass
{
private event EventOneDelegate EventOne;
private System.Threading.Thread m_workerThread;
private bool m_doWork;
private System.Windows.Threading.Dispatcher MainThreadDispatcher = null;
public ManagerClass()
{
// Assumes this is created on the main thread
MainThreadDispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
m_doWork = false;
m_workerThread = new System.Threading.Thread(DoThreadWork);
}
// Simple thread that raises an event every few seconds
private void DoThreadWork()
{
DateTime dtStart = DateTime.Now;
TimeSpan fiveSecs = new TimeSpan(0, 0, 5);
while (m_doWork)
{
if ((DateTime.Now - dtStart) > fiveSecs)
{
System.Diagnostics.Debug.Print("Raising event...");
try
{
if (EventOne != null)
{
// Tried calling the event delegate directly
EventOne();
// Tried synchronously invoking the event delegate from the main thread's dispatcher
MainThreadDispatcher.Invoke(EventOne, new object[] { });
// Tried asynchronously invoking the event delegate from the main thread's dispatcher
System.Windows.Threading.DispatcherOperation dispOp = MainThreadDispatcher.BeginInvoke(EventOne, new object[] { });
// Tried synchronously invoking the event delegate from the worker thread's dispatcher.
// Asynchronously invoking the event delegate from the worker thread's dispatcher did not work regardless of whether Excel is in edit mode or not.
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(EventOne, new object[] { });
}
}
catch (System.Exception ex)
{
// No exceptions were thrown when attempting to raise the event when Excel is in edit mode
System.Diagnostics.Debug.Print(ex.ToString());
}
dtStart = DateTime.Now;
}
}
}
// Method should be called from the main thread
[ComVisible(true), Description("Implements MethodAAA")]
public String MethodAAA(String strValue)
{
if (EventOne != null)
{
try
{
// Tried calling the event delegate directly
EventOne();
// Tried asynchronously invoking the event delegate from the main thread's dispatcher
System.Windows.Threading.DispatcherOperation dispOp = System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(EventOne, new object[] { });
// Tried synchronously invoking the event delegate from the main thread's dispatcher
System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(EventOne, new object[] { });
}
catch (System.Exception ex)
{
// No exceptions were thrown when attempting to raise the event when Excel is in edit mode
System.Diagnostics.Debug.Print(ex.ToString());
}
return "";
}
return "";
}
[ComVisible(true), Description("Start thread work")]
public String StartThreadWork(String strIn)
{
m_doWork = true;
m_workerThread.Start();
return "";
}
[ComVisible(true), Description("Stop thread work")]
public String StopThreadWork(String strIn)
{
m_doWork = false;
m_workerThread.Join();
return "";
}
}
}
我使用 regasm 注册它:
%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\regasm /codebase ComServerTest.dll /tlb:ComServerTest.tlb
Excel VBA 客户端代码:
Public WithEvents managerObj As ComServerTest.ManagerClass
Public g_nCounter As Long
Sub TestEventsFromWorkerThread()
Set managerObj = New ComServerTest.ManagerClass
Dim dtStart As Date
dtStart = DateTime.Now
g_nCounter = 0
Debug.Print "Start"
' Starts the worker thread which will raise the EventOne event every few seconds
managerObj.StartThreadWork ""
Do While True
DoEvents
' Loop for 20 secs
If ((DateTime.Now - dtStart) * 24 * 60 * 60) > 20 Then
' Stops the worker thread
managerObj.StopThreadWork ""
Exit Do
End If
Loop
Debug.Print "Done"
End Sub
Sub TestEventFromMainThread()
Set managerObj = New ComServerTest.ManagerClass
Debug.Print "Start"
' This call will raise the EventOne event
managerObj.MethodAAA ""
Debug.Print "Done"
End Sub
' EventOne handler
Private Sub managerObj_EventOne()
Debug.Print "EventOne " & g_nCounter
g_nCounter = g_nCounter + 1
End Sub
编辑 27/11/2014 - 我一直在对此进行更多调查。
此问题也发生在引发 COM 事件的 C++ MFC 自动化服务器上。如果我在 Excel 处于编辑模式时从主线程引发 COM 事件,则永远不会调用事件处理程序。服务器上不会抛出任何错误或异常,类似于我的 C# COM 服务器。 但是,如果我使用全局接口表将事件接收器接口从主线程 back 编组到主线程,然后调用事件 - 它会在 Excel 运行时阻塞在编辑模式。 (我还使用 COleMessageFilter 来禁用繁忙对话框而不响应对话框,否则我会收到异常:RPC_E_CANTCALLOUT_INEXTERNALCALL 在消息过滤器中调用是非法的。)
(如果您想查看 MFC 自动化代码,请告诉我,为简洁起见,我将跳过它)
知道了这一点,我尝试在我的 C# COM 服务器上做同样的事情。我可以实例化全局接口表(使用 pinvoke.net 中的定义)和消息过滤器(使用 MSDN 中的 IOleMessageFilter 定义)。但是,当 Excel 处于编辑模式时,该事件仍然“丢失”并且不会阻塞。
以下是我修改 C# COM 服务器的方法:
namespace ComServerTest
{
// Global Interface Table definition from pinvoke.net
[
ComImport,
InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("00000146-0000-0000-C000-000000000046")
]
interface IGlobalInterfaceTable
{
uint RegisterInterfaceInGlobal(
[MarshalAs(UnmanagedType.IUnknown)] object pUnk,
[In] ref Guid riid);
void RevokeInterfaceFromGlobal(uint dwCookie);
[return: MarshalAs(UnmanagedType.IUnknown)]
object GetInterfaceFromGlobal(uint dwCookie, [In] ref Guid riid);
}
[
ComImport,
Guid("00000323-0000-0000-C000-000000000046") // CLSID_StdGlobalInterfaceTable
]
class StdGlobalInterfaceTable /* : IGlobalInterfaceTable */
{
}
public class ManagerClass : IManagerClass
{
//...skipped code already mentioned in earlier sample above...
//...also skipped the message filter code for brevity...
private Guid IID_IDispatch = new Guid("00020400-0000-0000-C000-000000000046");
private IGlobalInterfaceTable m_GIT = null;
public ManagerClass()
{
//...skipped code already mentioned in earlier sample above...
m_GIT = (IGlobalInterfaceTable)new StdGlobalInterfaceTable();
}
public void FireEventOne()
{
// Using the GIT to marshal the (event?) interface from the main thread back to the main thread (like the MFC Automation server).
// Should we be marshalling the ManagerEvents interface pointer instead? How do we get at it?
uint uCookie = m_GIT.RegisterInterfaceInGlobal(this, ref IID_IDispatch);
ManagerClass mgr = (ManagerClass)m_GIT.GetInterfaceFromGlobal(uCookie, ref IID_IDispatch);
mgr.EventOne(); // when Excel is in edit mode, event handler is never called and does not block, event is "lost"
m_GIT.RevokeInterfaceFromGlobal(uCookie);
}
}
}
我希望我的 C# COM 服务器以类似于 MFC 自动化服务器的方式运行。这可能吗?我想我应该在 GIT 中注册 ManagerEvents 接口指针,但我不知道该怎么做?我尝试使用 Marshal.GetComInterfaceForObject(this, typeof(ManagerEvents)) 但这只会引发异常:System.InvalidCastException: Specified cast is not valid.
【问题讨论】:
-
在 C# 代码中,在
EventOne中添加类似Debug.Print的调试跟踪。调用MainThreadDispatcher.BeginInvoke(EventOne, ...)后是否看到跟踪? -
@Noseratio 对不起,我不明白。我无法在
EventOne中放置调试跟踪,因为它是一个委托,并且它引用的方法位于 VBA 代码 (managerObj_EventOne) 中。也许我没有清楚地理解你,你能用一些代码来说明吗?谢谢。 -
我的意思是:
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => { Debug.Print("Hello!"); EventOne(); }), new object[] { });你看到“你好!”吗?以这种方式在调试输出中? -
@Noseratio 感谢您的澄清。是的,我确实看到了“你好!”打印在调试输出中,但在 Excel 中编辑单元格时不会调用 VBA 事件处理程序。
-
现在我假设在 Excel 处于编辑模式时 VBA 事件处理程序没有连接,但我没有很好的解释为什么你的进程外服务器阻塞直到 Excel 是退出编辑模式。