带定时器的 Windows 服务 [英] Windows service with timer
问题描述
我在 c#.net 中创建了一个带计时器的 Windows 服务.当我在 Visual Studio 中调试/构建项目时它工作正常,但安装后它不执行其操作.
I have created a windows service with timer in c#.net. it works fine while i debug/build the project in visual studio but it does not perform its operation after installation.
这背后的原因可能是什么?
What might be the reason behind this ?
代码:
public partial class Service1 : ServiceBase
{
FileStream fs;
StreamWriter m_streamWriter;
Timer tm = new Timer();
public Service1()
{
InitializeComponent();
this.ServiceName = "timerservice";
tm.Interval = 2000;
tm.Tick += new EventHandler(PerformOperations);
tm.Start();
fs = new FileStream(@"c:mcWindowsService.txt", FileMode.OpenOrCreate, FileAccess.Write);
m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
}
private void PerformOperations(object sener, EventArgs e)
{
//StreamWriter swr = new StreamWriter("c:\test_from_database.txt",true);
try
{
OdbcConnection con = new OdbcConnection("DSN=liquor_data");
OdbcDataAdapter adp = new OdbcDataAdapter("", con);
DataSet ds = new DataSet();
string sql = "select * from item_group";
adp.SelectCommand.CommandText = sql;
adp.Fill(ds, "item_group");
foreach (DataRow dr in ds.Tables["item_group"].Rows)
{
// swr.Write(dr["group_name"].ToString() + " " + DateTime.Now.TimeOfDay.ToString() + "
");
//Console.WriteLine(dr["group_name"].ToString() + " " + DateTime.Now.TimeOfDay.ToString() + "
");
m_streamWriter.WriteLine(dr["group_name"].ToString() + " " + DateTime.Now.TimeOfDay.ToString() + "
");
}
m_streamWriter.Flush();
}
catch (Exception ex)
{
// swr.Write("Error :"+ ex.Message + " " + DateTime.Now.TimeOfDay.ToString() + "
"); }
}
}
}
推荐答案
Windows 服务的第一种方法并不容易..
First approach with Windows Service is not easy..
很久以前,我写了一个 C# 服务.
A long time ago, I wrote a C# service.
这是Service类的逻辑(经过测试,工作正常):
This is the logic of the Service class (tested, works fine):
namespace MyServiceApp
{
public class MyService : ServiceBase
{
private System.Timers.Timer timer;
protected override void OnStart(string[] args)
{
this.timer = new System.Timers.Timer(30000D); // 30000 milliseconds = 30 seconds
this.timer.AutoReset = true;
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);
this.timer.Start();
}
protected override void OnStop()
{
this.timer.Stop();
this.timer = null;
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
MyServiceApp.ServiceWork.Main(); // my separate static method for do work
}
public MyService()
{
this.ServiceName = "MyService";
}
// service entry point
static void Main()
{
System.ServiceProcess.ServiceBase.Run(new MyService());
}
}
}
我建议您在单独的静态方法中编写真正的服务工作(为什么不,在控制台应用程序中...只需添加对它的引用),以简化调试和清理服务代码.
I recommend you write your real service work in a separate static method (why not, in a console application...just add reference to it), to simplify debugging and clean service code.
确保间隔足够,并且只在 OnStart 和 OnStop 覆盖中写入日志.
Make sure the interval is enough, and write in log ONLY in OnStart and OnStop overrides.
希望这有帮助!
这篇关于带定时器的 Windows 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!