【问题标题】:Execute a .Net dll执行一个.Net dll
【发布时间】:2014-02-24 14:59:33
【问题描述】:

我想运行一个单独的进程并使用一个作为 dll 的 winforms 应用程序的 dll。这个问题之前已经被问过:

Is it possible to execute a .NET dll without an exe to load it?

由于它已经有几年的历史了,我想刷新这个问题。 .Net 框架中现在有什么可用的吗?这在技术上可行吗?我知道它在 java 中,所以我想知道 .Net 是否也可以这样做......

谢谢

更多信息:这是我想要实现的目标。现在有一段代码能够检测我们应用程序中的死锁。检测显然是在另一个胎面中运行的。因此,如果发生这种情况,我想运行另一个进程,向用户显示一个对话框并通知他有关死锁的信息,然后终止死锁的应用程序。

我希望它是一个能够向用户显示对话框的不同进程,因为大多数时候主线程将参与死锁,例如。由于 gui 主线程被阻塞,我无法显示某些内容。

【问题讨论】:

标签: c# .net dll process


【解决方案1】:

这已被多次回答:

来自Killercam

 // 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();

来自Code4life

在解决方案资源管理器中,右键单击引用并选择添加引用。这是您添加自定义设计的 C# DLL 的地方。

现在打开 Program.cs,并进行以下更改:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using ****your DLL namespace here****
namespace WindowsFormsApplication2
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new [****your startup form (from the DLL) here****]);
        }
    }
}

【讨论】:

  • 感谢您的回复。我试过了,但是当我调试应该在不同进程中运行的表单调用时,它的 ID 与我的应用程序 (Process.GetCurrentProcess().Id) 相同。还更新了我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
  • 2020-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多