Consider the following snippet:
if(form1.isLoggedIn) {
//Create a wait handle for the UI thread.
//the "false" specifies that it is non-signalled
//therefore a blocking on the waitone method.
AutoResetEvent hold = new AutoResetEvent(false);
filename = Path.Combine(Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData), DateTime.Now.ToString("ddMMyyhhmm") + "-" + form1.username);
Flow palm = new Flow(new FlowArguments(form1.username, filename), hold);
System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
System.Windows.Forms.ContextMenuStrip notificationIconContext = new System.Windows.Forms.ContextMenuStrip();
//Create a context menu strip
ToolStripMenuItem contextAbout = new ToolStripMenuItem("About");
ToolStripMenuItem contextExit = new ToolStripMenuItem("Exit");
//properties of notifyicon that SO dosnt care for including a event handler for the mouse click events.
//wait for the background thread to finish.
hold.WaitOne(Timeout.Infinite);
MessageBox.Show("Thankyou, exiting...");
form1.Dispose();
}
As you see The FLOW class is pretty simple. It just has a Threading.Timer which is run. Therefore on the UI thread i have to call WaitHandle.WaitOne() method, because otherwise the UI thread finishes and therefore ending the application..
My goal: Since there's no GUI, i want to create a notify icon. It works and everything,except I can't click on it and its not responsive, this is because it is created on the UI thread. I can't put it in the flow class because the Flow class is also on the UI thread (the timer in the Flow class is in background thread).
So how can I keep my notifyicon responsive while running the timer in the Flow class? I thought waithandle was my solution but since its a blocking method it doesn't work.
Any other ideas/solutions?
edit: response to Rich's answer: The Flow class is on the background thread and everything runs the way it should. But if I don't add the waithandle in the UI thread, the main() finishes on the UI thread therefore terminating the whole program. (even though the timer is enabled on the background thread). Is there another solution to waithandle? because I have to leave it in for the Flow class to do its thing. If I take it out, the program ends, if I leave it in it blocks the UI thread therefore my notifyicon dosnt work.