Http://www.csdn.net/Develop/Read_Article.asp? Id = 20312
VB given by Chapter E of MenciusCodeNow translate the code into C # for reference by people who are used to C #.
Note that the image must be in ICO format. Otherwise, the tray may not be displayed.
Using system;
Namespace ubiserialscontroller
{
/// <Summary>
/// Summary of class1.
/// </Summary>
Public class class1
{
Static system. serviceprocess. servicecontroller SC;
Static system. Windows. Forms. policyicon Ni;
Static system. Windows. Forms. contextmenu cm;
Static system. Timers. Timer timer;
Public class1 ()
{
//
// Todo: add the constructor logic here
//
}
Static void main ()
{
Try
{
SC = new system. serviceprocess. servicecontroller ("ubiserials ");
Ni = new system. Windows. Forms. policyicon ();
Ni. Visible = false;
Cm = new system. Windows. Forms. contextmenu ();
Cm. menuitems. Add (new system. Windows. Forms. menuitem ("stop", new eventhandler (stopservice )));
Cm. menuitems. Add (new system. Windows. Forms. menuitem ("Suspend", new eventhandler (pauseservice )));
Cm. menuitems. Add (new system. Windows. Forms. menuitem ("continue", new eventhandler (contiuneservice )));
Cm. menuitems. Add (new system. Windows. Forms. menuitem ("START", new eventhandler (startservice )));
Cm. menuitems. Add ("-");
Cm. menuitems. Add (new system. Windows. Forms. menuitem ("about", new eventhandler (about )));
Cm. menuitems. Add (new system. Windows. Forms. menuitem ("exit", new eventhandler (exit )));
Ni. contextmenu = cm;
Ni. Visible = true;
Setuptimer ();
System. Windows. Forms. application. Run ();
}
Catch (system. Exception ex)
{
System. Windows. Forms. MessageBox. Show (ex. Message. tostring ());
}
}
Private Static void stopservice (Object sender, system. eventargs E)
{
If (SC. Status = system. serviceprocess. servicecontrollerstatus. Running & SC. canstop = true)
{
Try
{
SC. Stop ();
}
Catch (system. Exception ex)
{
System. Windows. Forms. MessageBox. Show (ex. Message. tostring ());
}
}
}
Private Static void pauseservice (Object sender, system. eventargs e)
{< br> If (SC. status! = System. serviceprocess. servicecontrollerstatus. paused & SC. canpauseandcontinue = true)
{< br> try
{< br> SC. pause ();
}< br> catch (system. exception ex)
{< br> system. windows. forms. messageBox. show (ex. message. tostring ();
}< BR >}
Private Static void contiuneservice (Object sender, system. eventargs e)
{< br> If (SC. status = system. serviceprocess. servicecontrollerstatus. paused & SC. canpauseandcontinue = true)
{< br> try
{< br> SC. continue ();
}< br> catch (system. exception ex)
{< br> system. windows. forms. messageBox. show (ex. message. tostring ();
}< BR >}
Private Static void startservice (Object sender, system. eventargs E)
{
If (SC. Status = system. serviceprocess. servicecontrollerstatus. Stopped)
{
Try
{
SC. Start ();
}
Catch (system. Exception ex)
{
System. Windows. Forms. MessageBox. Show (ex. Message. tostring ());
}
}
}
Private Static void about (Object sender, system. eventargs E)
{
System. Windows. Forms. MessageBox. Show ("2005.4.3", "about ");
}
Private Static void exit (Object sender, system. eventargs E)
{
Try
{
Timer. Dispose ();
SC. Dispose ();
Cm. Dispose ();
Ni. Dispose ();
System. Windows. Forms. application. Exit ();
}
Catch (system. Exception ex)
{
System. Windows. Forms. MessageBox. Show (ex. Message. tostring ());
}
}
Private Static void timer_elapsed (Object sender, system. Timers. elapsedeventargs E)
{
Getservicestate ();
}
Private Static void getservicestate ()
{
SC. Refresh ();
Switch (SC. Status)
{
Case System. serviceprocess. servicecontrollerstatus. Stopped:
{
Ni. Icon = new system. Drawing. Icon ("stopped. ICO ");
Cm. menuitems [0]. Enabled = false;
Cm. menuitems [1]. Enabled = false;
Cm. menuitems [2]. Enabled = false;
Cm. menuitems [3]. Enabled = true;
Break;
}
Case System. serviceprocess. servicecontrollerstatus. Running:
{
Ni. Icon = new system. Drawing. Icon ("started. ICO ");
Cm. menuitems [0]. Enabled = true;
Cm. menuitems [1]. Enabled = true;
Cm. menuitems [2]. Enabled = false;
Cm. menuitems [3]. Enabled = false;
Break;
}
Case System. serviceprocess. servicecontrollerstatus. paused:
{
Ni. Icon = new system. Drawing. Icon ("paused. ICO ");
Cm. menuitems [0]. Enabled = false;
Cm. menuitems [1]. Enabled = false;
Cm. menuitems [2]. Enabled = true;
Cm. menuitems [3]. Enabled = false;
Break;
}
Default:
{
Ni. Icon = new system. Drawing. Icon ("paused. ICO ");
Break;
}
}
}
Private Static void setuptimer ()
{
Timer = new system. Timers. Timer ();
Timer. interval = 500;
Timer. elapsed + = new system. Timers. elapsedeventhandler (timer_elapsed );
Timer. Start ();
}
}
}