从在运行时在控制台应用程序中导入的DLL加载时形成冻结

时间:2015-03-10 16:19:47

标签: c# .net winforms dll console-application

继续上一个问题:Trying to show a Windows Form using a DLL that is imported at the runtime in a console application

我使用@ Ksv3n上一个问题的答案中给出的代码显示的表格正在冻结(在它上面显示一个等待光标)。有关代码,请查看以上链接。

1 个答案:

答案 0 :(得分:0)

尝试使用BackgroundWorker,如下所示:

(我没有编译代码,这是方法的一个例子)

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();
            }
        }
   }
}

在线资源:Background Worker for Beginners