我试过搜索和搜索这个,但也许我正在寻找错误的东西。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 "";
}
对不起,我的水晶球在修理,你会需要显示一些代码 – nvoigt
完成后,第一段代码在timer_tick中,因为它是一个以特定时间间隔运行的调度程序 – Oxholm