VS2005开发windows服务的注意事项
来源:互联网 发布:淘宝仓库中的宝贝 编辑:程序博客网 时间:2024/05/05 05:23
1、Timer控件失效的解决
在Windows 服务不引发计时器事件
原因:
我认为windows服务里不能使用System.Windows.Forms的Timer控件,Windows.Forms 计时器组件用于 Windows.Forms 环境。 WindowsForms 计时器组件不用于服务器环境,所以要使用System.Timer下的控件。
解决方案
这个可以通过修改控件的类型,把所有使用服务器计时器从命名空间 System.Timers 代替 System.Windows.Forms 计时器。注意System.Timers的事件是Elapsed事件
如:
Service1.Designer.cs
1namespace MyNewService
2{
3 partial class MyNewService
4 {
5 /// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 组件设计器生成的代码
52
53 private System.Diagnostics.EventLog eventLog1;
54 private System.Timers.Timer timer1;
55
56 }
57}
2{
3 partial class MyNewService
4 {
5 /// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 组件设计器生成的代码
52
53 private System.Diagnostics.EventLog eventLog1;
54 private System.Timers.Timer timer1;
55
56 }
57}
Service1.cs:
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Diagnostics;
6using System.ServiceProcess;
7using System.Text;
8
9namespace MyNewService
10{
11 public partial class MyNewService : ServiceBase
12 {
13 public MyNewService()
14 {
15 InitializeComponent();
16 if (!System.Diagnostics.EventLog.SourceExists("MySource"))
17 {
18 System.Diagnostics.EventLog.CreateEventSource(
19 "MySource", "MyNewLog");
20 }
21 eventLog1.Source = "MySource";
22 eventLog1.Log = "MyNewLog";
23 }
24
25 protected override void OnStart(string[] args)
26 {
27 eventLog1.WriteEntry("In OnStart");
28 this.timer1.Enabled = true;
29
30
31 }
32
33 protected override void OnStop()
34 {
35 eventLog1.WriteEntry("In onStop.");
36 this.timer1.Enabled = false;
37 }
38
39 protected override void OnContinue()
40 {
41 eventLog1.WriteEntry("In OnContinue.");
42 }
43
44
45
46 private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
47 {
48 eventLog1.WriteEntry("timer1_Tick." + DateTime.Now.ToString());
49 }
50
51
52 }
53}
54
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Diagnostics;
6using System.ServiceProcess;
7using System.Text;
8
9namespace MyNewService
10{
11 public partial class MyNewService : ServiceBase
12 {
13 public MyNewService()
14 {
15 InitializeComponent();
16 if (!System.Diagnostics.EventLog.SourceExists("MySource"))
17 {
18 System.Diagnostics.EventLog.CreateEventSource(
19 "MySource", "MyNewLog");
20 }
21 eventLog1.Source = "MySource";
22 eventLog1.Log = "MyNewLog";
23 }
24
25 protected override void OnStart(string[] args)
26 {
27 eventLog1.WriteEntry("In OnStart");
28 this.timer1.Enabled = true;
29
30
31 }
32
33 protected override void OnStop()
34 {
35 eventLog1.WriteEntry("In onStop.");
36 this.timer1.Enabled = false;
37 }
38
39 protected override void OnContinue()
40 {
41 eventLog1.WriteEntry("In OnContinue.");
42 }
43
44
45
46 private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
47 {
48 eventLog1.WriteEntry("timer1_Tick." + DateTime.Now.ToString());
49 }
50
51
52 }
53}
54
- VS2005开发windows服务的注意事项
- VS2005开发windows服务的注意事项
- VS2005开发windows服务的注意事项
- VS2005开发的Windows服务程序实例
- VS2005开发的Windows服务程序实例
- VS2005开发注意事项
- vs2005 以上开发windows 服务的安装,卸载启动,停止批处理
- VS2005 ATL WINDOWS服务感想
- VS2005 ATL WINDOWS服务感想
- VS2005 ATL WINDOWS服务感想
- 关于windows服务编程注意事项
- VS2005 注意事项
- 使用vs2005 性能工具,分析Windows服务
- 编写Windows系统服务进程(VS2005 C++)
- 关于服务程序开发的几点注意事项
- VS2005单元测试的方法与注意事项
- windows系统服务程序的开发
- C#Windows服务程序的快速开发
- 关于vs2008的sort排序的问题?
- 叶落何处?
- c#面试题及答案
- Oracle数据库的归档日志写满磁盘空间解决办法
- 好文收藏
- VS2005开发windows服务的注意事项
- Build DLLs with MinGW(--kill-at)
- 安装完IIS了但是访问不了,老是500错误
- UBuntu中增加中文字符编码的方法
- 你说你最美丽、最艳丽,而且还是唯一的正确选择!我该怎么办?
- 如何使用Visual C++调试程序
- C++编程易范的错误
- VC 常见问题
- sql中的位操作