【发布时间】: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