【发布时间】:2019-01-27 20:15:38
【问题描述】:
我在 stackoverflow 上进行了很多谷歌搜索和搜索,但无法弄清楚如何在从 DLL 返回之前从动态调用的 DLL 中获取进度消息。假设 DLL 完成了 10%...我想展示一下进度。我习惯于只显示调用返回的内容,但在这种情况下,我需要进度。这是我调用 DLL 的代码(C#,WPF 项目)。只是好奇如何在返回之前取回消息......
string pathAndDLLFile = Path.GetFullPath(DLLName + ".DLL");
Assembly a = Assembly.LoadFile(pathAndDLLFile);
string typeField = DLLNamespace + "." + DLLClass;
Type myType = a.GetType(typeField);
MethodInfo testMethod = myType.GetMethod(DLLMethod);
object obj = Activator.CreateInstance(myType);
object c = testMethod.Invoke(obj, new Object[] { nodeChild });
这是我的 DLL...
using System.Xml;
namespace InitialTest2
{
public class Class1
{
public int test(XmlNode xmlTest)
{
int testCount = 0;
int progress = 0;
foreach (XmlAttribute attributeChild in xmlTest.Attributes)
{
if (attributeChild.Name != "name" && attributeChild.Name != "testNumber" && attributeChild.Name != "Estimate" && !attributeChild.Name.Contains("DLL"))
{
if ((attributeChild.Name.Contains("MinLimit")) || (attributeChild.Name.Contains("MaxLimit")) || (attributeChild.Name.Contains("Unit")))
{
// not the attribute value
}
else
{
testCount ++;
}
}
progress = progress + 1;
// ToDo: report progress back to the main ap
}
return testCount;
}
public int test3(XmlNode xmlTest)
{
return 3;
}
}
public class Class2
{
public int test2(XmlNode xmlTest)
{
return 2;
}
}
}
【问题讨论】:
-
嗯,是的。我们无能为力。我们对 dll、类以及您调用的特定方法一无所知。通常,有一些特定的方法可以报告进度(如果完全支持的话)。一个是你传递的回调方法,然后可能有一个要注册的事件,因为有 async/await 和 Tasks,所以有 IProgress ...但实际上,你需要告诉我们更多关于那个dll ...
-
发布我的 DLL(上图)。很简单。循环并处理测试。在每个处理的测试结束时,我需要向主应用程序报告,而不是结束调用。
-
@ScottS:您的
test方法返回单个int值,仅此而已。这种方法不能向调用者报告或返回除int值以外的任何内容。您希望如何以及在哪里获得局部变量progress的当前值?在foreach循环完成之前,您不会从该方法返回。 -
当您在 DLL 中使用 return 时,调用就结束了,对吗?我需要一种在通话正在进行但尚未完成时发送消息的方法。有没有办法多次返回数据?我需要在通话期间报告进度。
-
“有没有办法多次返回数据”?如果您返回我在回答中提到的可枚举序列或值流。
标签: c# wpf dynamic dll messaging