【问题标题】:WPF main app dynamically invoking a DLL...need messaging from the DLL prior to the return to the main appWPF 主应用程序动态调用 DLL...在返回主应用程序之前需要来自 DLL 的消息传递
【发布时间】: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


【解决方案1】:

这与动态调用无关,但事实是 tour test 方法返回单个 int 值,仅此而已。

这种方法不能向调用者报告或返回除int 值以外的任何内容。您希望如何以及在哪里获得局部变量progress 的当前值?在 foreach 循环完成之前,您不会从该方法返回任何内容。

如果您想从同步方法返回某种进度或至少几个值,您可以将其返回类型更改为 IEnumerable<int> 并迭代返回值,例如:

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<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);
}

【讨论】:

  • DLL可以做console.writeline吗?我需要在不结束对 DLL 的调用的情况下报告 DLL 中发生的进度。
  • Console.WriteLine 只是一个例子。重要的是该方法返回几个值。
  • 但是我需要在返回之前报告进度。
  • 产量 - 信不信由你 - 记录在 C# 编程指南中:docs.microsoft.com/en-us/dotnet/csharp/language-reference/…
  • @ScottS:如果您还有其他问题,请提出一个新问题。您不应该在 cmets 字段中提出其他问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多