的完整的答案如下:
問題:類,要寫入數據excel無法處理來自excel的「忙/拒絕」響應消息。
解決方法:如描述here
IMessageFilter
定義(自鏈接)實現IMessageFilter
接口:我的課的
namespace ExcelAddinMessageFilter
{
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct INTERFACEINFO
{
[MarshalAs(UnmanagedType.IUnknown)]
public object punk;
public Guid iid;
public ushort wMethod;
}
[ComImport, ComConversionLoss, InterfaceType((short)1),
Guid("00000016-0000-0000-C000-000000000046")]
public interface IMessageFilter
{
[PreserveSig, MethodImpl(MethodImplOptions.InternalCall,
MethodCodeType = MethodCodeType.Runtime)]
int HandleInComingCall([In] uint dwCallType, [In] IntPtr htaskCaller,
[In] uint dwTickCount,
[In, MarshalAs(UnmanagedType.LPArray)] INTERFACEINFO[]
lpInterfaceInfo);
[PreserveSig, MethodImpl(MethodImplOptions.InternalCall,
MethodCodeType = MethodCodeType.Runtime)]
int RetryRejectedCall([In] IntPtr htaskCallee, [In] uint dwTickCount,
[In] uint dwRejectType);
[PreserveSig, MethodImpl(MethodImplOptions.InternalCall,
MethodCodeType = MethodCodeType.Runtime)]
int MessagePending([In] IntPtr htaskCallee, [In] uint dwTickCount,
[In] uint dwPendingType);
}
}
IMessageFilter
實現部分(見鏈接):
#region IMessageFilter Members
int ExcelAddinMessageFilter.IMessageFilter.
HandleInComingCall(uint dwCallType, IntPtr htaskCaller, uint dwTickCount, ExcelAddinMessageFilter.INTERFACEINFO[] lpInterfaceInfo)
{
// We're the client, so we won't get HandleInComingCall calls.
return 1;
}
int ExcelAddinMessageFilter.IMessageFilter.
RetryRejectedCall(IntPtr htaskCallee, uint dwTickCount, uint dwRejectType)
{
// The client will get RetryRejectedCall calls when the main Excel
// thread is blocked. We can handle this by attempting to retry
// the operation. This will continue to fail so long as Excel is
// blocked.
// As an alternative to simply retrying, we could put up
// a dialog telling the user to close the other dialog (and the
// new one) in order to continue - or to tell us if they want to
// abandon this call
// Expected return values:
// -1: The call should be canceled. COM then returns RPC_E_CALL_REJECTED from the original method call.
// Value >= 0 and <100: The call is to be retried immediately.
// Value >= 100: COM will wait for this many milliseconds and then retry the call.
return 1;
}
int ExcelAddinMessageFilter.IMessageFilter.
MessagePending(IntPtr htaskCallee, uint dwTickCount, uint dwPendingType)
{
return 1;
}
#endregion
隨着IMessageFilter
接口定義和實現,我設置了STA Thread
和Timers.Timer
如下:
螺紋:
thread = new Thread(WriteToExcel);
thread.SetApartmentState(ApartmentState.STA);
計時器:
timer = new System.Timers.Timer();
timer.Interval = 2000;
timer.Elapsed += new ElapsedEventHandler(StartSTAThread_Handler);
其中StartSTAThread_Handler
定義爲:
void StartSTAThread_Handler(object source, ElapsedEventArgs e)
{
thread.Start();
thread.Join();
thread = null;
}
該線程調用我使用的方法寫入Excel並使用0123處理拒絕的消息上述接口。我必須做的最後一件事是完全符合OM的參考資格,也就是說;而不是:
Excel.Range rng = app.ActiveSheet.Range["range_name"];
rng.Copy(); // ERRROR: message filter's RetryRejectedCall is NOT called
我不得不使用完全合格的引用:
app.ActiveSheet.Range["range_name"].Copy // OK: calls RetryRejectedCall when excel dialog etc is showing
雖然這似乎爲我的工作需要,它似乎矛盾的另一個海報here描述的「二點規則」 ...
我認爲這很簡單,只需在循環中使用Thread.Sleep()和自己的條件即可。 – pylover
謝謝。我想避免那樣旋轉。 –