我使用的是一个异步委托,它调用一个将XML文件加载到xpathdocument中的方法。如果xml太大而无法装入内存,则它永远无法完成加载。如果xml文件成功加载到xpathdocument中,则下面的代码可以工作。我已经能够使用执行asyncxpath.endinvoke(result)语句的计时器事件来结束createdocument方法,但它不会停止xpathdocument的加载。我的结论是,我唯一能做的就是发出application.end语句来终止应用程序。是否有人知道如何停止blackbox操作,例如加载xpathdocument。

delegate bool AsyncXpathQueryCaller(string xmlfile

bool found = false;
AsyncXpathQueryCaller asyncXpath = new
AsyncXpathQueryCaller(CreateDocument);
IAsyncResult result = asyncXpath.BeginInvoke(xmlfile, null, null);
while (!result.IsCompleted)
{
result.AsyncWaitHandle.WaitOne(100, false);

}
found = asyncXpath.EndInvoke(result);


private bool CreateDocument (string xmlfile)
{
XPathDocument doc = new XPathDocument(xmlfile);
}

最佳答案

在加载并检查大小之前使用FileInfo如何?如果它太大,就跳过它。
像这样的:

FileInfo fi = new FileInfo(xmlfile);
if(fi.Length < /*some huge number*/)
{
  //load the file
}

08-03 13:55