执行,同时检查Word是否可见冻结 [英] Execution freezing while checking if Word is Visible
问题描述
我检查在执行某些任务之前,Word是否仍然可见。问题是执行只是冻结后,我关闭Word 2010的可见性检查。
I'm check to see if Word is still visible before I perform certain tasks. The problem is execution just freezes after I close Word 2010 on the check of the visibility. Does not occur with 2007.
//Initializing Word and Document
While(WordIsOpen())
{
}
//Perform Post Close Tasks
public bool WordIsOpen()
{
if(MyApp.Application.Visible)//Execution just freezes at this line after Word is not visible
return true;
else
return false;
}
之前有人看到此问题吗?
Anyone see this issue before?
有没有更好的方法检查这个?
Is there a better way to check this?
推荐答案
我的建议是声明一个sentinel标志: / p>
My suggestion would be to declare a sentinel flag:
private bool isWordApplicationOpen;
初始化 应用程序
实例,订阅其 离开
event,并从那里重置标志:
When initializing your Application
instance, subscribe to its Quit
event, and reset the flag from there:
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应用程序关闭,以下代码可能更合适:
Edit: Given that you only need to wait until the Word application is closed, the following code might be more suitable:
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);
}
}
这篇关于执行,同时检查Word是否可见冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!