How to correctly multithreaded DLL, called at runtime in C #

Everything,

I want to write a .dll plugin that will be used / called by a .NET application called at runtime. My.dll is WinForm and displays (calculated) expensive operations. A .dll call from the main application is called through the .NET System.Reflection . I have to provide the calling application NameSpace, Class, and the method I want to call.

I would like to multi-threadedly migrate my .dll so that it is more user-friendly and I am really familiar with BackgroundWorker s.

EDIT: expanding the question.

So, I call .dll as follows:

 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. } } 

So then in WinForm.dll I want to multithreadedly spend a lot of time working so that I can show what is happening. Therefore, in the .dll, using BackgroundWorker , I:

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

Then I have a DoWork event 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); } //... } 

Event `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; } 

In addition to the code above, I have a regular RunWorkerCompleted , etc.

The calling application was written to allow the user to call existing .NET.dll at runtime ..dll, which I am trying to assemble, is an intensive time process that will only be provided to specific users. I ran the above code, and the problem is that it will not correctly update the user interface. That is, you cannot manipulate (resize, click, etc.) the Form , and it does not print or process information until the very end of processing, when it only prints the final message several times. However, it correctly creates the XML files that I need. What am I doing wrong? Should I reference the .dll method from a separate thread?

Any help would be greatly appreciated. Thanks so much for your time.

+4
source share
1 answer

I am not sure what methods you are trying to execute in this dll. I assume that they are not asynchronous because your main thread (application) will stop until they are executed. A simple example can be demonstrated with Messagebox.show ("myMessage"); method. For example, how could you execute 3 message windows at once? this will not be possible without using multiple threads. Hope these methods help:

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

then if I call this method 3 times as:

enter image description here

Now I will run 4 threads in my program. The main thread plus 3 calls that I created.

+4
source

Source: https://habr.com/ru/post/1384254/


All Articles