我的建议是申报定点标志:从那里
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