Use a tray to control the C # Implementation of Windows Services

Source: Internet
Author: User

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 ();
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.