gpt4 book ai didi

c# - 如何停止 "black box"操作?

转载 作者:数据小太阳 更新时间:2023-10-29 02:13:18 25 4
gpt4 key购买 nike

我正在使用一个异步委托(delegate),它调用一个将 xml 文件加载到 XPathDocument 中的方法。如果 xml 太大而无法放入内存,它永远不会完成加载。如果 xml 文件成功加载到 XPathDocument 中,则下面的代码有效。我已经能够使用一个计时器事件来执行 asyncXpath.EndInvoke(result) 语句并用于结束 CreateDocument 方法,但它不会阻止 XPathDocument 加载。我的结论是,我唯一能做的就是发出 Application.End 语句来终止应用程序。有谁知道如何停止黑盒操作,例如加载 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
}

关于c# - 如何停止 "black box"操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4964534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com