本软件运行本来是要开机,电脑用户登陆(不是软件登陆)后才能运行,现在希望做一个服务,希望像SQL server那样,一开电脑就已经运行,在服务里面为自动启动。
     请问下,这个服务该怎么写?

解决方案 »

  1.   

    // 新建项目——Windows服务

    public class DemoService : System.ServiceProcess.ServiceBase
    {
    #region Main、构造及其他
    /// <summary> 
    /// 必需的设计器变量。
    /// </summary> 
    private System.ComponentModel.Container components = null;
    public DemoService()
    {
    // 该调用是 Windows.Forms 组件设计器所必需的。
    InitializeComponent();
    } // 进程的主入口点
    public static void Main()
    { System.ServiceProcess.ServiceBase[] ServicesToRun;
    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new UserService() };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    } /// <summary> 
    /// 设计器支持所需的方法 - 不要使用代码编辑器 
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    // 
    // UserService
    // 
    this.ServiceName = "demo_service"; } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #endregion #region OnStart、OnStart

    /// <summary>
    /// 设置具体的操作,以便服务可以执行它的工作。
    /// </summary>
    /// <param name="args"></param>
    protected override void OnStart(string[] args)
    {
    //TODO:正式测试时将下面的 static void Main() 中的代码拷贝到本处...
    string message = ""; try
    {
    #region 启动服务时的工作
     
    // ....

    #endregion
    }
    catch(Exception ex)
    {
    throw ex;
    }
    }
     
    /// <summary>
    /// 停止此服务。
    /// </summary>
    protected override void OnStop()
    {
    try
    {
    //...
    }
    catch(Exception ex)
    {
    throw ex;
    } } #endregion

    }
      

  2.   

    服务不难写,关键是写好后记得用installutil安装就是了
      

  3.   

    建一个服务项目,在onstart里起一个线程,然后把你要做事的代码统统写进线程,onstop:打完收工
      

  4.   

    建立好一个服务项目后,该怎么安装啊?按照installutil my.exe 这样都不行,说installutil不是内部命令之类的
        有点迷茫了,谁加一下我的MSN:[email protected]
      

  5.   

    installutil命令要在
    在VS工具->命令提示工具下运行

    在\WINNT\Microsoft.NET\Framework\v1.1.4322目录下运行
      

  6.   

    好像记得要再好好写一个类的吧
    记得是谁说过
    用INSTALLUTIL安装的时候只有写了这个类
    服务才能自动设置为 自动 模式
      

  7.   

    呵呵 在在VS工具->命令提示工具下运行提示已经成功了, 不过在服务里怎么没看到?
      

  8.   

    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/vbcon/html/vbwlkwalkthroughcreatingwindowsserviceapplication.htm
      

  9.   

    搭车问个问题WIN 服务程序能断点跟踪调试吗怎么调试啊
      

  10.   

    1.在你的Service项目中再加一个实现System.Configuration.Install.Installer的类,如ProjectInstaller.cs (注意[RunInstaller(true)]),这样就能使用installutil.exe安装了。如下:/// <summary>
    /// ProjectInstaller 的摘要说明。
    /// </summary>
    [RunInstaller(true)]
    public class ProjectInstaller : System.Configuration.Install.Installer
    {
    private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
    public System.ServiceProcess.ServiceInstaller serviceInstaller1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public ProjectInstaller()
    {
    // 该调用是设计器所必需的。
    InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何初始化
    } /// <summary> 
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    #region 组件设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
    this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
    // 
    // serviceProcessInstaller1
    // 
    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
    this.serviceProcessInstaller1.Password = null;
    this.serviceProcessInstaller1.Username = null;
    // 
    // serviceInstaller1
    // 
    this.serviceInstaller1.DisplayName = "demo_service";
    this.serviceInstaller1.ServiceName = "demo_service";
    this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
    // 
    // ProjectInstaller
    // 
    this.Installers.AddRange(new System.Configuration.Install.Installer[] {
    this.serviceProcessInstaller1,
    this.serviceInstaller1}); }
    #endregion
    }
      

  11.   

    WIN 服务程序的断点跟踪调试可以用好几种方法:
    1.使用 #if(DEBUG)// 进程的主入口点
    public static void Main()
    {
    // 调试开关#if(DEBUG)
    {
    DebugProgram();
    }
    #else
    {
    System.ServiceProcess.ServiceBase[] ServicesToRun; // 同一进程中可以运行多个用户服务。若要将
    //另一个服务添加到此进程,请更改下行
    // 以创建另一个服务对象。例如,
    //
    //   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
    //
    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new DemoService() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
    #endif
    }2.把windows服务项目先按照控制台项目编写,等调试完成了在修改成windows服务项目(注意:别忘了在控制台项目的最后写上Console.ReadLine();以使这个项目停下来等待你的访问。)3.开发完成,先安装服务,启动服务,然后在Microsoft Visual Studio .NET 开发环境下打开项目,选择“调试”——“进程”——(选中你的服务进程)——附加;就可以调试了;
      

  12.   

    用VC ATL写吧,偶宁愿相信VC的东西。。
      

  13.   

    一、windows服务承载 
    用vs.net制作一个windows服务的过程基本不超过10个步骤,所以我们不需要害怕。 
    1、建立一个新的windows服务项目RemoteServer1 
    2、打开Service1代码视图,找到OnStart部分,加入代码 System.Runtime.Remoting.RemotingConfiguration.Configure(AppDomain.CurrentDomain.BaseDirectory + "RemoteServer1.exe.config"); (不要遗漏AppDomain.CurrentDomain.BaseDirectory + ) 
    config和控制台方式的config是一样的,我们让这个windows服务做的仅仅是从config文件读出配置信息进行配置通道。别忘记添加配置文件。 
    3、切换到设计视图,右键-添加安装程序 
    4、切换到新生成的ProjectInstaller.cs设计视图,找到serviceProcessInstaller1对Account属性设置为LocalSystem,对serviceInstaller1的ServiceName属性设置为RemoteServer1(服务的名字),StartType属性设置为Automatic(系统启动的时候自动启动服务) 
    5、别忘记对添加RemoteObject的引用 
    6、建立一个新的安装项目RemoteServerSetup(我们为刚才那个服务建立一个安装项目) 
    7、右键-添加-项目输出-主输出-选择RemoteService1-确定 
    8、右键-视图-自定义操作-自定义操作上右键-添加自定义操作-打开应用程序文件夹-选择刚才那个主输出-确定 
    9、重新生成这个安装项目-右键-安装 
    10、在服务管理器中(我的电脑-右键-管理-服务和应用程序-服务)找到RemoteServer1服务,启动服务 
    现在就可以打开客户端测试了!
      

  14.   

    msdn搜windows service。说得很详细