Moo*_*ght 5 c# dll multithreading winforms
全部,
我希望编写一个插件 .dll 以供在运行时调用的 .NET 应用程序使用/调用。我的 .dll 是一个 WinForm 并显示正在执行的(计算成本高的)操作。从主应用程序调用的 .dll 是通过 .NET 调用的System.Reflection
。我必须向调用应用程序提供NameSpace、Class和我想要调用的方法。
我想对我的 .dll 进行多线程处理,以便它对 UI 更加友好,而且我只对BackgroundWorker
s非常熟悉。
编辑:问题的扩展。
所以,我调用 .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.
}
}
Run Code Online (Sandbox Code Playgroud)
那么在 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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有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);
}
//...
}
Run Code Online (Sandbox Code Playgroud)
“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;
}
Run Code Online (Sandbox Code Playgroud)
除了上面的代码,我还有通常的RunWorkerCompleted
等。
已编写调用应用程序以允许用户在运行时调用自己的 .NET .dll。我试图放在一起的 .dll 是一个时间密集型的,并且只会提供给特定用户。我已经运行了上面的代码,但问题是它不会正确更新用户界面。也就是说,您无法操作(调整大小、单击等)Form
并且它不会打印和处理信息,直到处理结束时它才多次打印最终消息。但是,它正确地生成了我需要的 .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();
}
Run Code Online (Sandbox Code Playgroud)
那么如果我将该方法调用 3 次为:
我现在将在我的程序上运行 4 个线程。主线程加上我创建的 3 个调用。