国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

用托盤控制windows服務(wù)的c#實(shí)現(xiàn)

2024-07-21 02:18:11
字體:
供稿:網(wǎng)友


收集最實(shí)用的網(wǎng)頁特效代碼!

孟子e章給出的vb代碼,現(xiàn)在從這個(gè)代碼翻譯成c#,給習(xí)慣c#的人參考
需要注意的是圖片一定需要是ico格式,否則可能會(huì)導(dǎo)致托盤的不顯示

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: 在此處添加構(gòu)造函數(shù)邏輯
//

}

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("繼續(xù)",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("關(guān)于",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","關(guān)于");
}

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



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 岳普湖县| 恩平市| 北碚区| 鹤峰县| 灵台县| 清原| 五河县| 道孚县| 黔南| 徐水县| 亳州市| 江陵县| 长沙县| 枣强县| 瑞丽市| 惠安县| 丰宁| 游戏| 老河口市| 深水埗区| 华安县| 陆丰市| 临夏县| 凤台县| 郓城县| 桑日县| 大厂| 合水县| 郴州市| 渭南市| 岑巩县| 南充市| 青岛市| 鄂托克旗| 安岳县| 巴林左旗| 崇州市| 藁城市| 鄯善县| 温泉县| 龙州县|