Question

I've tried searching and searching for this, but maybe I'm searching for the wrong thing.

I am making a kind of controlpanel, where I can add some DLLs to be dynamically loaded. It's no problem for me to load the DLLs and no problem to get it running - I'm using "Activator.CreateInstance" to do this at the moment.

The method is doing exactly what I want it to do.

But... I need some help to a couple of things:

1: When I'm executing the DLLs, the Form is freezing, even though I'm running it in a thread - How do I avoid this?

2: I need to be able to read the current status of the DLL, live - The DLL files are made with a Superclass, so I can read a "CurrentStatus"-property. Is it possible to read this status of the running files? It seems to me, as the program is waiting for the DLL to finish, and that makes it freeze.

Hopefully some of you can help me.

Thanks in advance :)

Edit: Adding some code

                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 "";
    }
Was it helpful?

Solution

Well the form is pausing because this code

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

is forcing it to wait, even though the actual dll code is running on another thread.

You need to let the method finish. (Or I suppose you could use Application.DoEvents() if it doesn't leave a bad taste in your mouth.

To get the status information you should use an event that you can hook up after creating the dll. That way your app can be notified when the dll changes it's status.

Alternatively you could use a second timer and poll each running job for it's status, but events would be better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top