Creating a Windows Service with C#, concluded - Using the SysTray Class
(Page 4 of 4 )
The last thing on our list is to send our TimeServiceController program to the system tray when it is minimized. We will also want to show a balloon with a message if the service status changes unexpectedly. Create a new class file called SysTray.cs in the TimeServiceController project and paste in the code from vbAccelerator.com: http://www.vbaccelerator.com/home/NET/Code/Libraries/Shell_Projects/
SysTray/SysTray_Code_zip_SysTrayTester_SysTray_cs.asp.
Add the vbAccelerator.Components.Shell namespace to your Form1 class file:
using vbAccelerator.Components.Shell;
Add a SysTray object as a class field:
private SysTray sysTray;
Initialize the SysTray object at the end of the constructor:
this.sysTray = new SysTray(this);
iconList.Images.Add(new Icon("../../App.ico"));
this.sysTray.IconImageList = iconList;
this.sysTray.IconIndex = 0;
Now we need to wire up our form to keep track of when it has been minimized so we can hide it and add it to the task bar. Add this line to the end of InitializeComponent() method:
this.SizeChanged += new System.EventHandler(DoResize);
The Minimize() method looks like this:
private void DoResize(object sender, System.EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.sysTray.ShowInSysTray = true;
this.Hide();
}
}
If you run the project now you will see that when you minimize, it does indeed show up on your system tray and the main window vanishes from the taskbar altogether-–but clicking on the system tray icon does not restore it. That’s because we haven’t created an event handler for the sysTray.DoubleClick event. Add this line in the constructor:
this.sysTray.DoubleClick += new EventHandler(DoRestore);
And add this method:
private void DoRestore(object sender, EventArgs e)
{
this.sysTray.ShowInSysTray = false;
if (!this.Visible)
{
this.Visible = true;
}
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
this.BringToFront();
}
Now when you run your controller application you can minimize it to the system tray and double click the system tray icon to restore it. The last thing we need to do is add a bit of code to our timed method UpdateStatus() to check for an unexpected service status and show a balloon tooltip. Since we will be stopping and starting the service with the buttons in our application, any service status change, when minimized, should warrant a popup balloon. Thanks to vbAccelerator.com’s SysTray class, this is pretty simple:
private void UpdateStatus(object sender,
System.Timers.ElapsedEventArgs e)
{
string status = this.serviceController1.Status.ToString
();
if (this.Visible == false)
this.sysTray.ShowBalloonTip(status);
this.StatusLabel.Text = status;
}
To test this tooltip out, run the application and minimize it. Then go to Computer Management -> Services and stop the TimeServerService. You will see a balloon popup notification within ten seconds!
The End
All done! We’ve done quite a bit in these past three articles and I hope you learned something you didn’t already know. We have created a basic TCP server and installed it as a Windows Service with configurable logging and a small application to stop and start the service that runs minimized on the system tray and notifies us of services status changes. You can use this service as a foundation for a lot of interesting stuff, and I encourage you to experiment with building a more advanced server and client and to add more complex features to the service controller.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |