- 热门文章:
- · ASP.NET的实时天气及24小时天气预报(C#)
- · 使用C#代码实现增加用户帐号
- · 用c#写的smtp邮件发送类
- · C# Builder 实现POP3信箱的监视
- · 一个有KeepConnection开关的C#的Database类
- · 一个用C#写的词法分析程序
- · 利用自定义事件实现不同窗体间的通讯 -- C#篇
- · 用C#开发.NET CF 蓝牙通信模块
- · 关于C#下写的Web Service 服务在Delphi下调用时的问题
- · 用C#写的一个简单屏幕保护程序
- · .Net/C#: 利用反射编写通用的 rss 2.0 的 reader
- · C# Programming guidlines
上一篇:用C#生成中文汉字验证码的基本原理 >>
用托盘控制windows服务的c#实现
需要注意的是图片一定需要是ico格式,否则可能会导致托盘的不显示
using System;
namespace UBISerialsController
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class Class1
{
static System.ServiceProcess.ServiceController sc;
static System.Windows.Forms.NotifyIcon ni;
static System.Windows.Forms.ContextMenu cm;
static System.Timers.Timer timer;
public Class1()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
static void Main()
{
try
{
sc=new System.ServiceProcess.ServiceController("UBISerials");
ni=new System.Windows.Forms.NotifyIcon();
ni.Visible=false;
cm=new System.Windows.Forms.ContextMenu();
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("停止",new EventHandler(StopService)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("暂停",new EventHandler(PauseService)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("继续",new EventHandler(ContiuneService)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("开始",new EventHandler(StartService)));
cm.MenuItems.Add("-");
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("关于",new EventHandler(About)));
cm.MenuItems.Add(new System.Windows.Forms.MenuItem("退出",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)
{
if(sc.Status!=System.ServiceProcess.ServiceControllerStatus.Paused&&sc.CanPauseAndContinue==true)
{
try
{
sc.Pause();
}
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
}
}
}
private static void ContiuneService(object sender,System.EventArgs e)
{
if(sc.Status==System.ServiceProcess.ServiceControllerStatus.Paused&&sc.CanPauseAndContinue==true)
{
try
{
sc.Continue();
}
catch(System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
}
}
}
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","关于");
}
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();
}
}
}
下一篇:ASP.NET的实时天气及24小时天气预报(C#) >>
相关文章:
- · QQ验证码识别源代码(C#/NET1.1)
- · C#教学经验谈(3):储蓄计算器的源程序
- · C#实现Window管道技术
- · C#教学经验谈(2):会跑的按钮
- · c#取得汉字的拼音的首字母
- · C#做的一个加密/解密的类
- · c#发送需要smtp认证的邮件
- · c#中ref和out参数使用时需要注意的问题
- · 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- · C#代码
- · 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分
- · c#.net常用的小函数和方法集
- · RC2加密算法在C#的应用----完善版
- · 简单的c#文本文件读写
- · c#.net常用的小函数和方法集
- · 用Visual C#打造个性化的IE浏览器
- · 如何用UltraEdit编译C#源程序
- · .Net/C#: 实现支持断点续传多线程下载的 Http Web 客户端工具类 (C# DIY HttpWebClient)
- · Visual C#网络编程之TCP
- · C# Programming Guidelines
- · C#设计模式之原型(ProtoType)
- · C#设计模式之抽象工厂(AbstractFactory)
- · C# Coding Standard
- · c#.net常用的小函数和方法集
- · c#.net函数列表.txt
- · c#.net常用的小函数参考
- · C#中使用XML——编写XML
- · C#版ftp方法实现类库代码
- · Visual C# .NET 入门
- · 在C#后代码里使用IE WEB Control TreeView
- · C#里操作时间的例子!
- · 在C#中利用SharpZipLib进行文件的压缩和解压缩
- · C#中使用XML——基于DOM的案例分析
- · 远程重启计算机(C#)
- · C#中使用XML——实现DOM
- · C#中使用XML——编写XML
- · 用C#实现生成PDF文档
- · 使用C#编写的一个定时关机程序
