C# WPF主应用程序动态调用DLL…需要在返回到主应用程序之前从DLL发送消息
我在stackoverflow上做了大量的谷歌搜索,但是在从DLL返回之前,我不知道如何从动态调用的DLL返回进度消息。假设DLL完成了10%…我想展示一下进度。我习惯于只显示调用返回的内容,但在这种情况下,我需要进度。这是我调用DLL的代码(C#,WPF项目)。只是想知道如何在返回之前返回消息C# WPF主应用程序动态调用DLL…需要在返回到主应用程序之前从DLL发送消息,c#,wpf,dynamic,dll,messaging,C#,Wpf,Dynamic,Dll,Messaging,我在stackoverflow上做了大量的谷歌搜索,但是在从DLL返回之前,我不知道如何从动态调用的DLL返回进度消息。假设DLL完成了10%…我想展示一下进度。我习惯于只显示调用返回的内容,但在这种情况下,我需要进度。这是我调用DLL的代码(C#,WPF项目)。只是想知道如何在返回之前返回消息 string pathAndDLLFile = Path.GetFullPath(DLLName + ".DLL"); Assembly a = Assembly.LoadFile(pathAndDLL
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;
}
}
}
这与动态调用无关,但事实上,tour
test
方法只返回一个int
值,而不返回其他值
这种方法不能向调用者报告或返回除int
值以外的任何内容。您希望如何以及在何处获取局部变量的当前值progress
?在foreach
循环完成之前,不会从该方法返回任何内容
如果要从同步方法返回某种进度或至少几个值,可以将其返回类型更改为IEnumerable
,并迭代返回值,例如:
public IEnumerable<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++;
}
}
yield return progress + 1;
// ToDo: report progress back to the main ap
}
yield return testCount;
}
公共IEnumerable测试(XmlNode xmlTest)
{
int testCount=0;
int progress=0;
foreach(xmlTest.Attributes中的xmldattributechild)
{
if(attributeChild.Name!=“Name”&&attributeChild.Name!=“testNumber”&&attributeChild.Name!=“Estimate”&&attributeChild.Name.Contains(“DLL”))
{
if((attributeChild.Name.Contains(“最小限制”))| |(attributeChild.Name.Contains(“最大限制”))| |(attributeChild.Name.Contains(“单位”))
{
//不是属性值
}
其他的
{
testCount++;
}
}
收益回报进度+1;
//ToDo:向主ap报告进度
}
收益率-收益率;
}
用法:
IEnumerable<int> c = testMethod.Invoke(obj, new Object[] { nodeChild }) as IEnumerable<int>;
if (c != null)
{
int count;
foreach (var progress in c)
{
Console.WriteLine(progress);
count = progress;
}
Console.WriteLine(count);
}
IEnumerable c=testMethod.Invoke(obj,新对象[]{nodeChild})作为IEnumerable;
如果(c!=null)
{
整数计数;
foreach(c中的var进度)
{
控制台写入线(进度);
计数=进度;
}
控制台写入线(计数);
}
嗯,是的。我们无能为力。我们对dll、类以及您正在调用的特定方法一无所知。通常,有一些特定的方法来报告进度(如果支持的话)。一个是您传递的回调方法,然后可能有一个要注册的事件,因为有async/Wait和Tasks,所以有IProgress。。。但实际上,你需要告诉我们更多关于那个dll的信息…发布我的dll(上图)。很简单。循环并处理测试。在每次处理的测试结束时,我需要向主应用程序报告,而不必结束调用返回。@ScottS:Yourtest
方法返回一个int
值,而不返回任何其他值。这种方法不能向调用者报告或返回除int
值以外的任何内容。您希望如何以及在何处获取局部变量的当前值progress
?直到foreach
循环完成后,才从方法返回。在DLL中使用return时,调用结束,对吗?我需要一种在通话进行中但尚未结束时发送消息的方法。是否有一种方法可以多次返回数据?我需要在通话过程中汇报进度。“有没有办法多次返回数据”?如果你像我在回答中提到的那样返回一个可枚举的序列或一个值流,DLL能做一个console.writeline吗?我需要在不结束对DLL的调用的情况下报告DLL中发生的进度。Console.WriteLine只是一个例子。重要的是,该方法返回几个值。但我需要在返回之前报告进度。不管你信不信,收益率记录在C#programming guide:@ScottS:如果你有其他问题,请问一个新问题。您不应该在“评论”字段中提出其他问题。