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);
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.