分享

如何在C#中正确多线程运行时调用的DLL

 印度阿三17 2019-06-26

所有,

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

我想多线程我的.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是一个时间密集的,只能提供给特定用户.我已经运行了上面的代码,问题是它不会正确更新用户界面.也就是说,你不能操作(调整大小,点击等)表格,它不会打印和进展信息,直到处理结束时它只打印多次最终消息.但是,它正确生成我需要的.xml文件.我究竟做错了什么?我应该从一个单独的线程调用.dll方法吗?

任何帮助将不胜感激.非常感谢你花时间陪伴.

解决方法:

我不确定你尝试在那个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个调用.

来源:https://www./content-1-269401.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多