-3

Continuing from my previous question: Trying to show a Windows Form using a DLL that is imported at the runtime in a console application

The form I am showing using the code given in the answer to my previous question by @Ksv3n is freezing (showing a wait cursor over it). For code please check out above link.

Community
  • 1
  • 1
Aishwarya Shiva
  • 3,460
  • 15
  • 58
  • 107
  • Have you tried to do that with a non console application? Like having a form with a button that do your Main method code? I think it could be that your process isn't getting plugged into the Windows form message pump. – Juan Mar 10 '15 at 16:23
  • possible duplicate of [Trying to show a Windows Form using a DLL that is imported at the runtime in a console application](http://stackoverflow.com/questions/28967874/trying-to-show-a-windows-form-using-a-dll-that-is-imported-at-the-runtime-in-a-c) – MethodMan Mar 10 '15 at 16:27
  • @MethodMan that's my question – Aishwarya Shiva Mar 10 '15 at 16:33
  • @Juan I am trying to create an API that loads DLL GUIs. – Aishwarya Shiva Mar 10 '15 at 16:34
  • Acutally it seems a duplicate of this [How to run a winform from console-application](http://stackoverflow.com/questions/277771/how-to-run-a-winform-from-console-application) – Juan Mar 10 '15 at 16:36
  • @Juan No its not because I am importing the DLL at runtime and then loading the Windows form from it. Read my previous question. – Aishwarya Shiva Mar 10 '15 at 17:26
  • You are loading the DLL at runtime, but from a Console application, which is the same as the other question cover. Have you tried to do as that questions suggests? The [STAThread] thing looks key – Juan Mar 10 '15 at 17:30

1 Answers1

0

Try to use a BackgroundWorker, like this:

(I dind't compiled the code, this is an example for the approach)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
namespace DLLTest
{
    class Program
    {
        static void Main(string[] args)
        {
            BackgroundWorker m_oWorker;
            m_oWorker = new BackgroundWorker();

            m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);

            m_oWorker.RunWorkerAsync();

            Console.ReadLine();
        }

        static void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string DLLPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\TestLib.dll";
            var DLL = Assembly.LoadFile(DLLPath);

            foreach (Type type in DLL.GetExportedTypes())
            {
                dynamic c = Activator.CreateInstance(type);
                c.test();
            }
        }
   }
}

Online Resource:Background Worker for Beginners

Stefano Bafaro
  • 915
  • 10
  • 20