2013-04-17 25 views
1

我试过搜索和搜索这个,但也许我正在寻找错误的东西。C#加载程序集和实时更新表格

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

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

但是......我需要几件事情一定的帮助:

1:当我执行的DLL形式冻结,即使我在一个线程中运行它 - 怎么办我避免这个?

2:我需要能够读取DLL的当前状态,live - 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 ""; 
    } 
+0

对不起,我的水晶球在修理,你会需要显示一些代码 – nvoigt

+0

完成后,第一段代码在timer_tick中,因为它是一个以特定时间间隔运行的调度程序 – Oxholm

回答

1

好形式暂停,因为此代码

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

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

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

为了得到你应该使用一个事件的状态信息,您可以创建DLL后挂钩。这样,你的应用程序可以当DLL改变其状态的通知。

另外,您可以使用第二个定时器和轮询每个正在运行的任务为它的地位,但事件可能会更好。

+0

我使用DL中的事件Ls,所以这是一个完美的答案! 我添加了一个额外的事件,告诉DLL是否已完成运行,并且正在“父级”中处理这个事件。 非常感谢! :) – Oxholm

+0

@Oxholm,没问题。很高兴我能帮上忙 –

相关问题