我的建議是申報定點標誌:從那裏
private bool isWordApplicationOpen;
當初始化你Application
例如,訂閱其Quit
事件,並重置標誌:
MyApp = new Word.Application();
MyApp.Visible = true;
isWordApplicationOpen = true;
((ApplicationEvents3_Event)MyApp).Quit +=() => { isWordApplicationOpen = false; };
// ApplicationEvents3_Event works for Word 2002 and above
然後,在你的循環,只需檢查標誌是否設置:
while (isWordApplicationOpen)
{
// Perform work here.
}
編輯:既然你只需要等待Word應用程序被關閉,下面的代碼可能更適合:
using (ManualResetEvent wordQuitEvent = new ManualResetEvent(false))
{
Word.Application app = new Word.Application();
try
{
((Word.ApplicationEvents3_Event)app).Quit +=() =>
{
wordQuitEvent.Set();
};
app.Visible = true;
// Perform automation on Word application here.
// Wait until the Word application is closed.
wordQuitEvent.WaitOne();
}
finally
{
Marshal.ReleaseComObject(app);
}
}
什麼類型'MyApp'? –
Microsoft.Office.Interop.Word.Application MyApp – ExceptionLimeCat
這是否僅在調試會話期間發生?我想知道是否winword.exe被連接到VS,它導致它阻止MyApp.Application.Visible。如果您在未經調試的情況下運行項目會發生什麼 – Dai