本软件运行本来是要开机,电脑用户登陆(不是软件登陆)后才能运行,现在希望做一个服务,希望像SQL server那样,一开电脑就已经运行,在服务里面为自动启动。
请问下,这个服务该怎么写?
请问下,这个服务该怎么写?
解决方案 »
- 第二张贴,进程的关闭,软件的关闭,在线等
- c# 中内存释放的问题----在线等,高手来
- this.ConnectAccessDB(); 这句我可不可以这样写 Page.ConnectAccessDB();
- 怎样在c#代码里实现先弹alert,再弹confirm
- 大家有没有把excel导入数据库的c#代码阿??
- C#掉用API,帮忙啊!
- c# 将程序打包成exe,如何连接数据库
- 关于输入类型的练习 求改错
- 请教visual studio bate2中文版安装问题?
- ComBox的SelectedIndexChanged触发问题
- 没分了,Excel.Application oExcel=new Excel.Application()无法创建抽象类或接口“Excel.Application”的实例
- 3个Timer的区别(System.Threading.Timer,System.Windows.Forms.Timer,System.Timers.Timer)
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
}
有点迷茫了,谁加一下我的MSN:[email protected]
在VS工具->命令提示工具下运行
或
在\WINNT\Microsoft.NET\Framework\v1.1.4322目录下运行
记得是谁说过
用INSTALLUTIL安装的时候只有写了这个类
服务才能自动设置为 自动 模式
/// 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
}
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 开发环境下打开项目,选择“调试”——“进程”——(选中你的服务进程)——附加;就可以调试了;
用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服务,启动服务
现在就可以打开客户端测试了!