您的位置:首页 > 其它

创建windows服务后台读取订单数量并更新XML文件

2009-01-08 14:44 330 查看
前言:编写一个Windows服务程序,定时从后台读取订单数量并更新XML文件。

测试环境:Visual Studio 2005 SP1、Windows Server 2003 SP2

一、新建项目

打开VS2005,新建一个“Windows 服务”项目。

二、添加Timer控件

展开“工具箱”,在“组件”标签下找到“Timer”双击,这时就添加了一个Timer组件

必须注意:我明明是从“组件”下添加的“Timer”应该来自“System.Timers命名空间”(“System.Timers.Timer”才能在Windows服务程序中正常定时调用),但是现在Timer却继承至“System.Windows.Forms.Timer”。所以得修改“.Designer.cs”文件,一开始怎么都不能实现定时读取数据库,为此搞了一下午才搞明白,很是郁闷啊

修改如下:

#region 组件设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要

/// 使用代码编辑器修改此方法的内容。

/// </summary>

private void InitializeComponent()

{

//this.timer1 = new System.Windows.Forms.Timer(this.components);这是原来的

this.timer1 = new System.Timers.Timer();

((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();

//

// timer1

//

this.timer1.Enabled = true;

//this.timer1.Interval = 15000;

this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);

//

// Service1

//

this.ServiceName = "Service1";

((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();

}

#endregion

private System.Timers.Timer timer1;这是改过的

//private System.Windows.Forms.Timer timer1;这是原来的

三、添加配置文件

服务每次调用配置文件,取一些基本参数,这样一些变更就可直接修改配置文件而不必修改代码或者不用重新编译服务即能实现修改。添加应用程序配置文件(app.config),该文件存放在编译后项目中“Bin\Debug\”下:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

</configSections>

<connectionStrings>

</connectionStrings>

<appSettings>

<add key="DataString" value="Server=111.111.111.111;Database=LwTicket;Integrated Security=false;User Id=sa;Password=@@@@;"/>

<add key="xmlfile" value="D:\\flight_noEdit\\flight\\statxml.xml"/>

<add key="procedureName" value="sp_Check_Stat_byservice"/>

<add key="timeInterval" value="15000"/>

</appSettings>

</configuration>

四、service1.cs文件(具体实现方法)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.ServiceProcess;

using System.Text;

using System.Data.SqlClient;

using System.Threading;//使用线程

namespace WindowsRemind

{

public partial class Service1 : ServiceBase

{

SqlConnection objSqlConnection = null;

SqlCommand objSqlCommand = null;

statXml Stat_Xml = null;

SqlbaseHelper sqlhelper = null;

public Service1()

{

InitializeComponent();

}

protected override void OnStart(string[] args)

{

// TODO: 在此处添加代码以启动服务。

this.timer1.Enabled = true;

this.timer1.Interval = Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["timeInterval"].ToString());

//AddEventLog("Start");

this.tUpdateOrderStat();

}

protected override void OnStop()

{

// TODO: 在此处添加代码以执行停止服务所需的关闭操作。

this.timer1.Enabled = false;

}

public void AddEventLog(string msgStr)

{

if (!EventLog.SourceExists("AlertEvent"))

{

EventLog.CreateEventSource("AlertEvent", "AlertEvent");

}

EventLog myLog = new EventLog();

myLog.Source = "AlertEvent";

myLog.WriteEntry(msgStr, EventLogEntryType.Information);

}

private void tUpdateOrderStat()

{

Thread t = new Thread(new ThreadStart(UpdateOrderStat));

t.Start();

}

public void UpdateOrderStat()

{

string statStr = GetStatStr().ToString();

Stat_Xml = new statXml();

string statStaus = Stat_Xml.UpdateStatXML(statStr.ToString()).ToString();

if (statStaus.ToString().ToUpper() == "TRUE")

{

AddEventLog("更新状态文件成功!");

}

else

{

AddEventLog("更新状态文件失败!" + statStaus.ToString());

}

}

public string GetStatStr()

{

string StatusStr = "";

try

{

sqlhelper=new SqlbaseHelper();

objSqlConnection = sqlhelper.GetSqlConnection();

objSqlCommand = new SqlCommand();

objSqlCommand.Connection = objSqlConnection;

objSqlConnection.Open();

objSqlCommand.CommandType = CommandType.StoredProcedure;

objSqlCommand.CommandText = System.Configuration.ConfigurationSettings.AppSettings["procedureName"].ToString();

SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter(objSqlCommand);

SqlParameter returnstr = objSqlCommand.Parameters.Add("@returnStr", SqlDbType.VarChar, 50);

returnstr.Direction = ParameterDirection.Output;

objSqlCommand.ExecuteNonQuery();

StatusStr = objSqlCommand.Parameters["@returnStr"].Value.ToString();

}

catch (Exception ex)

{

StatusStr = "0@0@0@0@0";

AddEventLog(ex.Message.ToString());

}

finally

{

objSqlCommand.Dispose();

objSqlConnection.Dispose();

objSqlConnection.Close();

}

return StatusStr;

}

(一开始写在timer1_Click里面,后来发现也是不能实时调用)

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

this.tUpdateOrderStat();

}

}

}

五、最后编译该服务

在设计模式下右键-->添加安装程序-->设置serviceProcessInstaller1的Account为LocalSystem

设置serviceInstaller1的StartType为Automatic

在解决方案资源管理器中右键进行生成

启动“运行”——CMD打开命令提示符

进入C:\Windows\Microsoft.Net\Framework\v2.0.50727

这里有一个InstallUtil.exe文件

键入InstallUtil.exe 项目文件夹\bin\Debug\xx.exe并回车,命令开始执行,服务安装

另外如果之前添加过此服务,则必须先调用InstallUtil.exe /u 进行服务删除才能安装
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐