【问题标题】:Form freezing when loaded from a DLL that is imported at runtime in a console application从运行时在控制台应用程序中导入的 DLL 加载时窗体冻结
【发布时间】:2015-03-10 16:19:46
【问题描述】:

继续我之前的问题:Trying to show a Windows Form using a DLL that is imported at the runtime in a console application

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

【问题讨论】:

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


【解决方案1】:

尝试使用 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

【讨论】:

    猜你喜欢
    • 2015-05-12
    • 2010-11-19
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    相关资源
    最近更新 更多