Visual C#中调用Windows服务初探

儒雅的国际刑警

儒雅的国际刑警

2016-02-19 16:16

只要你有一台电脑或者手机,都能关注图老师为大家精心推荐的Visual C#中调用Windows服务初探,手机电脑控们准备好了吗?一起看过来吧!

  Windows服务是独立于登录用户而工作的Windows应用程序,它通常在计算机启动时开始执行,且常常连续执行,直到计算机关闭为止。像Exchange Server,IIS和杀毒软件等都使用这种方式,这样就可以独立于某一用户而且可以在任何用户登录前来运行,同时也可以服务于所有的进程,从而以一种服务的形式存在。

  正因为Windows服务有着这么多的特性,因此,当需要一些特殊功能的时候就可以考虑使用Windows服务来解决问题。比如下面我们要做的这个例子。对于我们这些程序设计人员,计算机是在一起工作时间最长的伙伴,每天都会对着它的屏幕八个小时以上,还不包括下班后在家打游戏的时间,因此,保护眼睛是最重要的了。问题的起因

  Windows服务就做完了,余下的就是要测试了,但发现这个EXE无法运行,它会提示你该EXE需要使用安装程序来安装服务,看来不可能写一个程序就算是Windows服务了,还要把它注册到Windows才行。

  接下来,右击CareEye.cs的设计视图,添加安装程序,(VS.NET想得就是挺周到的),这下又出来一批代码,不过好在不用改代码了,只要把Account的账户类型设成LocalSystem,把StartType设成手动启动就行了,这里用手动是为了方便调试,以后可以改成自动类型。

  编译完后,还是无法运行,此处还需要一步,就是运行installutil来安装这个服务,其安装和卸载的用法为:

  

installutil CareEye.exeinstallutil /u CareEye.exe 

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  安装完后能过系统的服务管理器你就可以看到你的服务了,只要点击启动就可以把它启动,把时间向前改一个小时它就会提醒你需要休息了,呵呵,够简单了吧。

  如果你想制作成安装包分发给自己的朋友,只需要再添加个部署项目就行了,不过为了完成自注册,要在自定义操作编辑器中的安装阶段添加一个自定义的安装操作,把InstallerClass属性设成TRUE即可。

  余下的事情,就是自己动手试试吧,这回不用担心用眼超时了!

  以下是careeye.cs的源程序:

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  

using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.ServiceProcess;using System.Threading;using System.Windows.Forms;namespace CareEye{ public class CareEye : System.ServiceProcess.ServiceBase {  private Thread MainThread;  /// summary  /// 必需的设计器变量。  /// /summary  private System.ComponentModel.Container components = null;  public CareEye()  {   // 该调用是 Windows.Forms 组件设计器所必需的。   InitializeComponent();   // TODO: 在 InitComponent 调用后添加任何初始化   MainThread=new Thread(new ThreadStart(ThreadFunc));   MainThread.Priority=ThreadPriority.Lowest;  }  // 进程的主入口点  static void Main()  {   //System.ServiceProcess.ServiceBase[] ServicesToRun;   // 同一进程中可以运行多个用户服务。若要将   //另一个服务添加到此进程,请更改下行   // 以创建另一个服务对象。例如,   //   // ServicesToRun = New System.ServiceProcess.ServiceBase[] {new CareEye(), new MySecondUserService()};   //   //ServicesToRun = new System.ServiceProcess.ServiceBase[] { new CareEye() };   System.ServiceProcess.ServiceBase.Run(new CareEye());  }  /// summary  /// 设计器支持所需的方法 - 不要使用代码编辑器  /// 修改此方法的内容。  /// /summary  private void InitializeComponent()  {   //   // CareEye   //   this.ServiceName = "CareEye";  }  /// summary  /// 清理所有正在使用的资源。  /// /summary  protected override void Dispose( bool disposing )  {   if( disposing )   {    if (components != null)    {     components.Dispose();    }   }   base.Dispose( disposing );  }  /// summary  /// 设置具体的操作,以便服务可以执行它的工作。  /// /summary  protected override void OnStart(string[] args)  {   // TODO: 在此处添加代码以启动服务。   MainThread.Start();  }  /// summary  /// 停止此服务。  /// /summary  protected override void OnStop()  {   // TODO: 在此处添加代码以执行停止服务所需的关闭操作。   MainThread.Abort();  }  public static void ThreadFunc()  {   int LastHour=DateTime.Now.Hour;   while (true)   {    System.Threading.Thread.Sleep(60000);    if (DateTime.Now.Hour-1==LastHour)    {     MessageBox.Show("为了爱护您的眼睛,请您暂时休息5分钟并向远处眺望!","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1,MessageBoxOptions.DefaultDesktopOnly);LastHour=DateTime.Now.Hour;    }   }  } }} 

展开更多 50%)
分享

猜你喜欢

Visual C#中调用Windows服务初探

编程语言 网络编程
Visual C#中调用Windows服务初探

Visual C#中调用Windows API的要点

编程语言 网络编程
Visual C#中调用Windows API的要点

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

Visual C#实现Windows信使服务

编程语言 网络编程
Visual C#实现Windows信使服务

用Visual C#调用Windows API函数

电脑网络
用Visual C#调用Windows API函数

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

C#中调用Windows API的要点

电脑网络
C#中调用Windows API的要点

索引服务调用代码(C#)

电脑网络
索引服务调用代码(C#)

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

在Javascript中实现伪哈希表

在Javascript中实现伪哈希表

seup.exe进程是什么

seup.exe进程是什么
下拉加载更多内容 ↓