创建windows服务并注册后,启动服务时系统总是提示‘服务启动后又停止了’。我是第一次写服务,请高手帮忙解答。以下是服务源码:
我是想通过服务调用一个winfrom的程序
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;namespace WsBrowsePages
{
public partial class BOpenService2 : ServiceBase
{
bool servicePaused;
private Process proc;
public BOpenService2()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
proc = new Process();
proc.StartInfo.FileName = "D:\\BrowsePages\\BrowsePages\\bin\\Debug\\BrowsePages.exe";
proc.StartInfo.Password = null;
proc.StartInfo.UserName = null;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
FileSystemWatcher curWatcher = new FileSystemWatcher();
} protected override void OnStop()
{
//// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
proc.Close();
proc.Dispose();
} protected override void OnPause()
{
servicePaused = true;
}
protected override void OnContinue()
{
servicePaused = false;
}
}
}using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;namespace WsBrowsePages
{
[RunInstaller(true)]
public class Installer1 : System.Configuration.Install.Installer
{
/// 
/// 必需的设计器变量.
/// 
private System.ComponentModel.Container components = null;
private System.ServiceProcess.ServiceProcessInstaller spInstaller;
private System.ServiceProcess.ServiceInstaller sInstaller;
public Installer1()
{
// 该调用是设计器所必需的.
InitializeComponent();
// TODO: 在 InitComponent 调用后添加任何初始化
}
#region Component Designer generated code /// 
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容.
/// 
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// 创建ServiceProcessInstaller对象和ServiceInstaller对象 this.spInstaller =
new System.ServiceProcess.ServiceProcessInstaller();
this.sInstaller = new System.ServiceProcess.ServiceInstaller();
// 设定ServiceProcessInstaller对象的帐号、用户名和密码等信息
this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.spInstaller.Username = null;
this.spInstaller.Password = null;
// 设定服务名称
this.sInstaller.ServiceName = "BOpenService2";
// 设定服务的启动方式
this.sInstaller.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.Installers.AddRange(
new System.Configuration.Install.Installer[] { this.spInstaller,this.sInstaller });
} #endregion
}}

解决方案 »

  1.   


    windows service需要保证一个线程在运行吗?好像不要吧?那且不是做一个windows service 一定要浪费一个线程的资源?这说不过去。
      

  2.   

    停止的原因是因为你的程序报错导致的,你不妨把你的代码用trycacth包括起来,然后把错误输出到日志,看是什么错误,这样才能确定原因,不然你发帖子也不见得有人知道用windows service的一个不好的地方就是不好调试,但可接触日志来看