You can use
te.Join();
as the last line before Main ends. This should make it work.
Longer explanation to how it should be
When you start a windows form application, you start a GUI/Event/Main thread. Visualize an infinite while loop like below somewhere in the winforms library
Queue<EventArgs> EventQueue = new Queue<EventArgs>();
while (!StopRequested) {
if (EventQueue.Any())
ProcessEvent(EventQueue.Dequeue());
Thread.CurrentThread.Sleep(100);
}
All the controls are created in this thread and all the events are fired in this thread. Now since this code does not exist in your code, the only way to get a GUI/Event related service is by posting into this EventQueue
Now with this background, let's approach your situation.
Raising and Handling Events
Raising and handling events work the exact same way with one form/thread as it works with multiple forms/threads. Remember, raising events is simply the act of posting into that EventQueue
and calling of event handlers happens from within the ProcessEvents
Form Life-Cycle
In a typical WinForms application, you would use a line like below to tell the system to create an EventQueue
and associate its life-cycle with the life-cycle of the form.
Application.Run(new Form());
You can use
new Form().Show()
to start pumping events into the EventQueue
, consequently displaying the form, but remember that as soon as your application's main thread reaches the end of Main
, the EventLoop
will be abruptly terminated.
So closing this form will cause your application to stop, and vice-versa.
Your case
I would strongly advise you to launch the Form from the main thread and simply do the other work on a new thread. I see that you are using sender as an int
and an int[]
, which is a problem. Below is how I would write if I need to achieve the same goal. I have tried to keep it similar to your sample whenever I could
class Program {
static Form1 frm;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
frm = new Form1();
frm.Test += TestEventHandler;
new Thread(new ThreadStart(DoAsyncProcessing)).Start();
Application.Run(frm);
}
static void DoAsyncProcessing() {
// async code goes here
Thread.Sleep(5000);
// raise the event
frm.RaiseTestEvent("Test Event Data");
}
static void TestEventHandler(object sender, TestEventArgs e) {
System.Diagnostics.Debug.WriteLine("Received test event with data: "
+ e.EventData);
}
}
public partial class Form1 : Form {
public event EventHandler<TestEventArgs> Test;
public Form1() {
InitializeComponent();
}
public void RaiseTestEvent(string eventData) {
var arg = new TestEventArgs() { EventData = eventData };
OnTest(arg);
}
protected virtual void OnTest(TestEventArgs e) {
EventHandler<TestEventArgs> handler = Test;
if (handler != null)
handler(this, e);
}
}
public class TestEventArgs : EventArgs {
public object EventData { get; set; }
}