I am currently working on a modular application using multiple dynamically loaded plugins. The main window also works as an output for feedback messages from the various plugins. This is a central requirement so the plugins can't have their own output.
The main program consists of 3 classes:
Main class that creates the GUI and handles the plugin calls
Second class that collects the plugins from a specified folder using MEF
Export class that can be accessed from a Plugin to send a message (string[]) to show in the main window
The Export class uses an event for receiving messages, to which the main class subscribes and writes the message into a DataGridView.
This works fine as long as I start a plugin without putting it into a separate thread. But as expected the mainform is frozen while the plugin is working. Now I tried to create separate threads for the plugins so I can have multiple ones running in the background simultaneously. Unfortunately this renders the message receiving a cross-thread operation.
With the use of an event I intended to make it thread-safe because the mainform subscribes to the Event and handles the messages in the "main-thread". Obviously I was wrong… I still don't quite get how the event-triggered method in the main window suddenly switches into the separate task...
Some additional Information:
Invoking is not an option because the output window consists of a lot more controls than just the DataGridView and these controls are constantly being modified whenever a message is received.
The mainform has a public static string[] for transferring the message's content from the export class to the main class
Is there any way to put the message writing method into the main thread but still able to subscribe to the event that is fired from another thread? Or maybe is there another approach to the task?
[Export]
public class Exportklasse : Interfaces.IMain
{
public static event EventHandler MeldungEintragen;
// Diese Methode aufrufen, um von Modulen aus Textmeldungen einzutragen
public void MeldungEmpfangen(string[] MeldungInput, EventArgs e)
{
EventHandler EintragenEvent = MeldungEintragen;
if (EintragenEvent != null) {
MainForm.MeldungText = MeldungInput;
EintragenEvent(this, e);
}
}
}