阅读 Word 文档应用程序在 100 后崩溃+文件
我借用并修改了一个程序,如下所示。基本上问题是,经过一定数量的文件后,程序将崩溃。我追踪到了这个过程。不可避免的是,文档本身被证明是没有问题的,因此导致问题的原因是重复打开和关闭 Word 文档。而且它是随机的,有时会处理 100 个文件,有时会处理 800 个。有人有什么想法吗?当我在调试器中运行它时,我没有看到生成任何错误。该程序只是停止处理文件并变得无响应。我是否缺少一些垃圾收集?如何判断是否存在内存泄漏?
private string readFileContent(string path)
{
string docstring = "";
Word.ApplicationClass wordApp = new Word.ApplicationClass();
object file = path;
object nullobj = System.Reflection.Missing.Value;
Word.Document doc = wordApp.Documents.Open(
ref file, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
try
{
SProc.Println("Created word doc instance");
//doc.Clipboard.Clear();
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
SProc.Println("Copied text to clipboard");
//System.Threading.Thread.Sleep(500);
IDataObject data = Clipboard.GetDataObject();
docstring = data.GetData(DataFormats.Text).ToString();
txtFileContent.Text = docstring;
}
catch ( ConfigurationErrorsException e)
{
SProc.Println("Word Error:" +e.ToString());
}
finally
{
doc.Close(ref nullobj, ref nullobj, ref nullobj);
wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
}
return docstring;
}
I have a procedure which I borrowed and modified shown below. Basically the problem is that after some amount of files, the program will crash. I traced it to this procedure. Inevitably the document itself proved to be fine, so it is something about the repeated opening and closing of word docs which is causing issues. And it's random, sometimes it will process 100 files, sometimes 800. Anyone have any thoughts? When I run it in the debugger I do not see any errors generated. The program just stops processing files and becomes unresponsive. Is there some garbage collection that I am missing? How can I tell if there is a memory leak?
private string readFileContent(string path)
{
string docstring = "";
Word.ApplicationClass wordApp = new Word.ApplicationClass();
object file = path;
object nullobj = System.Reflection.Missing.Value;
Word.Document doc = wordApp.Documents.Open(
ref file, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
try
{
SProc.Println("Created word doc instance");
//doc.Clipboard.Clear();
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
SProc.Println("Copied text to clipboard");
//System.Threading.Thread.Sleep(500);
IDataObject data = Clipboard.GetDataObject();
docstring = data.GetData(DataFormats.Text).ToString();
txtFileContent.Text = docstring;
}
catch ( ConfigurationErrorsException e)
{
SProc.Println("Word Error:" +e.ToString());
}
finally
{
doc.Close(ref nullobj, ref nullobj, ref nullobj);
wordApp.Quit(ref nullobj, ref nullobj, ref nullobj);
}
return docstring;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另一个建议:
创建 Word.ApplicationClass 一次并在应用程序的生命周期中使用它,我怀疑您是否真的想要为 800 个文件启动它并关闭它 800 次。
Another suggestion:
Create the Word.ApplicationClass once and use it for the life of your application, I doubt you really want to start it and close it 800 times for 800 files.