【问题标题】:How to Correctly Multi-Thread a DLL Called at Run-Time in C#如何在 C# 中正确地对运行时调用的 DLL 进行多线程处理
【发布时间】:2011-12-02 05:44:55
【问题描述】:

全部,

我希望编写一个插件 .dll 以供运行时调用的 .NET 应用程序使用/调用。我的 .dll 是一个 WinForm 并显示正在执行的(计算上昂贵的)操作。从主应用程序调用的 .dll 是通过 .NET System.Reflection 调用的。我必须向调用应用程序提供我想要调用的 NameSpaceClassMethod

我想对我的 .dll 进行多线程处理,以便它对 UI 更友好,而且我只对 BackgroundWorkers 非常熟悉。

编辑:问题的扩展。

所以,我按如下方式调用 .dll:

if (classType != null)
{
    if (bDllIsWinForm)
    {
        classInst = Activator.CreateInstance(classType);
        Form dllWinForm = (Form)classInst;
        dllWinForm.Show();

        // Invoke required method.
        MethodInfo methodInfo = classType.GetMethod(strMethodName);
        if (methodInfo != null)
        {
            object result = null;
            // The method being called in this example is 'XmlExport'.
            result = methodInfo.Invoke(classInst, new object[] { dllParams });
            return result.ToString();
        }
    }
    else
    {
        // Else not a WinForm do simalar.
    }   
}

然后在 WinForm .dll 中,我想对耗时的工作进行多线程处理,以便显示正在发生的事情。所以在.dll中,使用BackgroundWorker我有:

BackgroundWorker bgWorker; // Global.    

public string XmlExport(object[] connectionString)
{
    try
    {
        bgWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
        bgWorker.DoWork +=  new DoWorkEventHandler(bgWorker_DoWork);
        bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);
        bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);

        // Wait until workerThread is done.
        threadDoneEvent = new AutoResetEvent(false);
        bgWorker.RunWorkerAsync();
        threadDoneEvent.WaitOne();
        return strResult; // Global String strResult
    }
    catch (Exception)
    {
        throw;
    }
}

然后我有DoWork 事件处理程序:

void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker thisWorker = sender as BackgroundWorker;
    strResult = (string)this.XmlExportBgw(ref thisWorker); // Or should I use bgWorker?
    e.Result = strResult;
}

public string XmlExportThreaded(ref BackgroundWorker thisWorker) 
{
    try
    {
        // Some expesive work...

        // UI.
        InfoBall infoBall = new InfoBall(); // Class containing processing information.
        // Set infoBall parameters here...
        (thisWorker as BackgroundWorker).ReportProgress(infoBall.progressBarValue, infoBall);

        // Some more expensive work...

        // UI.
        (thisWorker as BackgroundWorker).ReportProgress(infoBall.progressBarValue, infoBall);
    }
    //...
}

`ProgressChanged' 事件是

void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // UI.
    InfoBall someBall = (InfoBall)e.UserState;

   // Update process information.
   if (someBall.showRichTextBox)
   {
       this.richTextBox.AppendText(someBall.richTextBoxText);
       this.richTextBox.ScrollToCaret();
   }
   return;
}

除了上面的代码,我还有通常的RunWorkerCompleted 等。

已编写调用应用程序以允许用户在运行时调用其自己的 .NET .dll。我试图放在一起的 .dll 是一个时间密集型的,而且只提供给特定用户。我已经运行了上面的代码,问题是它不会正确更新用户界面。也就是说,您无法操作(调整大小、单击等)Form,并且它不会打印和处理信息,直到处理结束时它只会多次打印最终消息。但是,它正确生成了我需要的 .xml 文件。我究竟做错了什么?我应该从单独的线程调用 .dll 方法吗?

任何帮助将不胜感激。非常感谢您的宝贵时间。

【问题讨论】:

  • 删除 threadDoneEvent.WaitOne() 调用。启动一个线程然后等待它完全破坏了使用线程的原因。并阻止 RunWorkerCompleted 事件运行,即您要用来处理线程结果的事件。
  • 我不想在 .dll 完成工作之前将其传回给调用应用程序。这样,线程就完成了它的工作,并且显示 .dll 正在做什么的 WinForm 保持打开状态。主要问题是我的 .dll 的 UI 没有正确更新?非常感谢您的宝贵时间。
  • ProgessChanged 事件也不会触发。你等不及了。
  • 哦!好的,非常感谢您的宝贵时间。
  • 还有一件事。那么如何在 .dll 完成之前停止调用应用程序的继续?我是否创建另一个线程然后从中调用.dll,等待这个“主”线程?这里最好是什么?再次感谢...

标签: c# winforms multithreading dll


【解决方案1】:

我不确定您尝试在该 dll 中执行哪种方法。我假设它们不是异步的,这意味着您的主线程(应用程序)将停止,直到它们完成执行。 Messagebox.show("myMessage"); 可以演示一个简单的示例。方法。例如,如何一次执行 3 个消息框?不使用多个线程是不可能的。希望这些方法有所帮助:

    public void SomeMethod()
    {


        Thread thread = new Thread(new ThreadStart(() =>
        {
            // this code is going to be executed in a separate thread
            MessageBox.Show("hello");    

            // place additional code that you need to be executed on a separate thread in here            

        }));

        thread.Start();
    }

那么如果我将该方法调用 3 次为:

我现在将在我的程序上运行 4 个线程。主线程加上我创建的 3 个调用。

【讨论】:

  • 谢谢你,太好了,它提醒我以这种方式使用线程!但是我无法以这种方式启动 .dll,因为我无权访问调用应用程序源代码 - 我只能传递命名空间和类和方法。但是,假设我在 .dll 中的 WinForm 类是您的 MessageBoxes 之一,我也想用进度信息更新该消息框,这意味着我想多线程我的方法来完成所有工作,但向用户通过表单。我在考虑使用基于事件的异步模式?非常感谢您的回答。
猜你喜欢
  • 2013-05-03
  • 2016-03-21
  • 2020-10-24
  • 1970-01-01
  • 2013-01-24
  • 1970-01-01
  • 2013-11-24
  • 2016-10-13
  • 1970-01-01
相关资源
最近更新 更多