【问题标题】:C# load assembly and live update formC# 加载程序集和实时更新表单
【发布时间】:2013-04-17 08:23:04
【问题描述】:

我已经尝试过搜索和搜索这个,但也许我正在搜索错误的东西。

我正在制作一种控制面板,我可以在其中添加一些动态加载的 DLL。 加载 DLL 对我来说没问题,让它运行也没问题 - 我现在正在使用“Activator.CreateInstance”来执行此操作。

该方法正在做我想要它做的事情。

但是......我需要一些帮助:

1:当我执行 DLL 时,即使我在线程中运行表单,表单也会冻结 - 如何避免这种情况?

2:我需要能够实时读取 DLL 的当前状态 - DLL 文件是由超类创建的,因此我可以读取“CurrentStatus”属性。 是否可以读取正在运行的文件的这种状态? 在我看来,因为程序正在等待 DLL 完成,这使它冻结。

希望你们中的一些人可以帮助我。

提前致谢:)

编辑:添加一些代码

                string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, curJob.Scheduler_JobAssemblyName + ".dll");
                Assembly assembly = Assembly.LoadFile(path);

                GenericSchedulerJob o = (GenericSchedulerJob)Activator.CreateInstance(Type.GetType(curJob.Scheduler_JobAssemblyName + "." + curJob.Scheduler_JobAssemblyName + ", " + curJob.Scheduler_JobAssemblyName, true));
                Thread thread = new Thread(() => ExecuteDLL(ref o, "RunJob", row));
                thread.Start();
                while (!o.JobFinished)
                {
                    // Do something (update status, etc.)
                }


    private string ExecuteDLL(ref GenericSchedulerJob job, string methodName, DataGridViewRow row)
    {
        string returnVal;
        if (job != null)
        {
            // Get method
            MethodInfo mi = job.GetType().GetMethod(methodName); // Get method info
            if (mi != null)
            {
                // Let's start ...
                returnVal = (string)mi.Invoke(job, null); // Execute method with parameters
            // job is the SuperClass, where I can read the status from
            }
            else
            {
                returnVal = "Method <" + methodName + "> not found in class."; // Error .. 
            }
        }
        else
        {
            returnVal = "Class not found in external plugin."; // Error .. 
        }

        return "";
    }

【问题讨论】:

  • 对不起,我的水晶球正在维修,您需要出示一些代码。
  • 完成,第一段代码在timer_tick中,因为它是一个以特定间隔运行的调度程序

标签: c# dll .net-assembly dynamic-loading createinstance


【解决方案1】:

由于这段代码,表单正在暂停

            while (!o.JobFinished)
            {
                // Do something (update status, etc.)
            }

强制它等待,即使实际的 dll 代码正在另一个线程上运行。

您需要让方法完成。 (或者我想你可以使用Application.DoEvents(),如果它不会在你的嘴里留下不好的味道。

要获取状态信息,您应该使用可以在创建 dll 后连接的事件。这样,当 dll 更改其状态时,您的应用程序就会收到通知。

或者,您可以使用第二个计时器并轮询每个正在运行的作业以了解其状态,但事件会更好。

【讨论】:

  • 我让它在 DLL 中处理事件,所以这是一个完美的答案!我添加了一个额外的事件,告诉 DLL 是否已完成运行并在“父级”中处理它。非常感谢! :)
  • @Oxholm,没问题。很高兴我能帮上忙
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多