创建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
}}
我是想通过服务调用一个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
}}
解决方案 »
- 关于b/s与c/s通讯问题
- Developer Express .NET v8.2中GridControl打印如何设置列头显示为中文
- 后台代码执行的每一步都及时的显示在 richTextBox 控件里??
- 类似金山词霸一样的取词窗口(不是取词,只要求那样的提示窗口)
- 我装了vc 2003.net 运行第一个程序 hello怎么不行 是不是设置错了
- 编程珠玑中的字符反转算法。
- C# string[]如何转换成System.Array类型
- nhibernate一对多的问题(困扰好长时间了)
- 请问NotifyIcon将程序最小化到托盘后,如果得到托盘图标的x,y坐标?
- 求助关于某个表的某列与另一个表的某列关联的问题
- 高分望解决excel导入access问题
- SqlConnection
windows service需要保证一个线程在运行吗?好像不要吧?那且不是做一个windows service 一定要浪费一个线程的资源?这说不过去。