如何在运行时启动的.dll内启动Windows窗体 [英] How to launch a Windows Form from within a .dll launched at runtime

查看:224
本文介绍了如何在运行时启动的.dll内启动Windows窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了这一点,不能建立正确的方法。我的问题如下:我有一个winforms应用程序,从里面我希望启动一个时间intedive .dll。我可以使用System.Reflection这样做没有问题这样

  //使用反射(系统)从请求的.dll执行方法。反射)。 
//[System.Runtime.InteropServices.DllImport(strDllPath)]
DLL = Assembly.LoadFrom(strDllPath);
classType = DLL.GetType(String.Format({0}。{0},ListUfCmdParams [1]));
classInst = Activator.CreateInstance(classType);
XmlExpInfo = classType.GetMethod(DllParams [0]);
XmlExpInfo.Invoke(classInst,paramObj);

//返回一些东西。
返回String.Format(Method{0}'from'{1} {2}'successfully execution!,
ListUfCmdParams [2],ListUfCmdParams [1],strDotDll);

这个功能很好,但被调用的进程是如此的时间密集我想向用户显示什么发生。为了做到这一点,我已经在.dll文件中包含一个具有progressBar和一些其他属性的WinForm。当我这样做,我得到一个例外。当Activator.CreateInstance()尝试执行其工作时,会发生这种情况:MissingMethodException无法创建抽象类。我以前遇到这个错误,当我使用部分类,我不得不从我的类中删除partial关键字,以使.dll正确执行(我刚刚离开!)。我无法从上面的winForms类中删除这个partial关键字,所以问题是我如何从我的.dll中调用一个winform(如果确实有可能)?所以.dll可以从调用应用程序中显示进度?



感谢您的时间,



Nick



Ps。我已经阅读了以下内容,他们有点模糊:



一个可以从主应用程序启动的WinForms的DLL



等等。

解决方案

我刚刚看到这个问题,以为我会更新我最终如何做到这一点。 p>

最后,我发现以下是为了实现上述所需要的最有效的方法。首先你启动一个WinForm,它保存你的进度信息。第二个你从显示事件中唤醒你的工作者方法。



第一部分的代码,即使用Reflection调用WinForm的代码如下: p>

  //使用反射(System.Reflection)从请求的.dll执行方法。 
汇编DLL = Assembly.LoadFrom(strDllPath);
键入classType = DLL.GetType(String.Format({0}。{0},strNsCn));
对象classInst = Activator.CreateInstance(classType,paramObj);
表单dllWinForm =(Form)classInst;
dllWinForm.ShowDialog();

我希望这有助于别人。


I have researched this a fair bit and cannot establish the correct approach. My problem is as follows: I have a winForms applications and from within it I wish to launch a time intesive .dll. I can do this using System.Reflection no problem like this

    // Execute the method from the requested .dll using reflection (System.Reflection).
    //[System.Runtime.InteropServices.DllImport(strDllPath)]
    DLL = Assembly.LoadFrom(strDllPath);
    classType = DLL.GetType(String.Format("{0}.{0}", ListUfCmdParams[1]));
    classInst = Activator.CreateInstance(classType);
    XmlExpInfo = classType.GetMethod(DllParams[0]);
    XmlExpInfo.Invoke(classInst, paramObj);

    // Return something.
    return String.Format("Method '{0}' from '{1}{2}' successfully executed!", 
    ListUfCmdParams[2], ListUfCmdParams[1], strDotDll);

this works great but the process being called is so time intensive I want to display to the user what is happening. To do this I have included in the .dll file a WinForm which has a progressBar and some other attributes. When I do this I get an exception. This occurs when "Activator.CreateInstance()" attempts to do its work: MissingMethodException "Cannot create an abstract class". I have come across this error before when I using partial classes and I had to remove the "partial" keyword from my classes to enable the .dll to execute correctly (which I just about got away with!). I cannot remove this "partial" keyword from the above winForms class so, the question is "How do I call a winForm from within my .dll (if indeed it is possible)?" so that the .dll can show its progress as it executes from the calling application?

Thanks for your time,

Nick

Ps. I have read the following threads and they are somewhat ambiguous:

A DLL with WinForms that can be launched from A main app

et al.

解决方案

I have just seen this question again and thought I would update as to how I eventually did this.

In the end I found the following to be the most effective way of performing the above for what I wanted. First you launch a WinForm which holds your progress information. Second youu envoke your "worker" method from within the "Shown" event.

The code for the first part i.e. to call the WinForm using Reflection is provided below:

    // Execute the method from the requested .dll using reflection (System.Reflection).
    Assembly DLL = Assembly.LoadFrom(strDllPath);
    Type classType = DLL.GetType(String.Format("{0}.{0}", strNsCn));
    object classInst = Activator.CreateInstance(classType, paramObj);
    Form dllWinForm = (Form)classInst;  
    dllWinForm.ShowDialog();

I hope this helps someone else.

这篇关于如何在运行时启动的.dll内启动Windows窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆