using System;
using System.Data.SqlClient;
using System.ServiceProcess;
using Timer = System.Timers.Timer;
namespace WeiBo
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
Timer t=new Timer(10000);
t.Elapsed += GetWeiBo;//到時間的時候執行事件;
t.AutoReset = true;//設置是執行一次(false)還是一直執行(true);
t.Enabled = true;//是否執行System.Timers.Timer.Elapsed事件;
}
public void GetWeiBo(object source, System.Timers.ElapsedEventArgs e)
{
try
{
string ConnStr = Settings.Default.ConnStr;
SqlConnection conn = new SqlConnection(ConnStr);
SqlCommand comm = new SqlCommand("insert into tb1(str) values('111')", conn);
conn.Open();
int num=comm.ExecuteNonQuery();
conn.Close();
Utils.LogFile(string.Format("----成功插入{0}條----",num));
}
catch (Exception e1)
{
Utils.LogFile(e1.Message);
throw;
}
}
protected override void OnStart(string[] args)
{
Utils.LogFile("----開始採集----");
}
protected override void OnStop()
{
Utils.LogFile("----採集結束----");
}
}
}
windows服務每隔一段時間執行程序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.