windows服务timer问题

achao201 2008-12-10 10:08:49
本人想写一个windows服务,通过读取xml数据,把xml数据当参数传给指定的方法。。。,xml数据如下:

<Webs>
<Web>
<WebName>找供应</WebName>
<WebUrl>http://www.myweb.com/</WebUrl>
<WebTime>30</WebTime>
</Web>
<Web>
<WebName>找求购</WebName>
<WebUrl>http://www.myweb3.com/</WebUrl>
<WebTime>10</WebTime>
</Web>
<Web>
<WebName>找公司</WebName>
<WebUrl>http://www.myweb3.com/</WebUrl>
<WebTime>10</WebTime>
</Web>
</Webs>

现在有个问题是:xml 中的webtime是来控制定时调用方法的,比方说。十分钟就调用一次。。
我该怎么用timer来调用方法呢??是不是每个web数据的都要有一个timer控件来控制呢???请大家帮忙,最好能提供点相应的代码。。。。
...全文
210 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
achao201 2008-12-11
  • 打赏
  • 举报
回复
【我是楼主】大家说的都没有效果,请下看10楼的代码。。。。。
格拉 2008-12-11
  • 打赏
  • 举报
回复
学习
hangang7403 2008-12-11
  • 打赏
  • 举报
回复
up
dayizhixiaotutu 2008-12-11
  • 打赏
  • 举报
回复
放OnStart()里
dayizhixiaotutu 2008-12-11
  • 打赏
  • 举报
回复
服务属性 与桌面交互勾上
achao201 2008-12-11
  • 打赏
  • 举报
回复
代码放在构造函数里运行一下就中断了,而放在OnStart()里根本没法运行。。。
achao201 2008-12-11
  • 打赏
  • 举报
回复
说错了,不是中断而是运行不了。。。
achao201 2008-12-11
  • 打赏
  • 举报
回复
根据一楼的写了个,但是运行一下就中断了。。。,请大家帮忙分析一下原因,以及有什么好的解决方法。。。。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Xml;
using System.Threading;
using System.Web;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Data.SqlClient;


namespace WServiceMk_html
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();

System.Threading.Thread.Sleep(10000);
string filepath = System.Windows.Forms.Application.StartupPath + "\\XmlData.xml";

DataSet ds = new DataSet();
ds.ReadXml(filepath);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
//arylst.Clear();
System.Threading.Timer tm;
int WebTime = Convert.ToInt32(ds.Tables[0].Rows[i]["WebTime"].ToString());
ArrayList arylst = new ArrayList();
arylst.Add(ds.Tables[0].Rows[i]["WebName"].ToString());
arylst.Add(ds.Tables[0].Rows[i]["WebUrl"].ToString());
tm = new System.Threading.Timer(new TimerCallback(TimerTest), arylst, 0, WebTime * 6);

//System.Configuration.ConfigurationSettings.AppSettings
}
}

protected override void OnStart(string[] args)
{


}

private void TimerTest(object obj)
{
ArrayList list = (ArrayList)obj;

SqlConnection con = new SqlConnection("server=.;uid =sa;pwd=111111;database=dataTest");
con.Open();
string sql;
sql = "insert into TestTimer values('" + list[0].ToString() + "','" + list[1].ToString() + "')";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();

}


protected override void OnStop()
{

}




}
}



xml 数据该帖子提问的数据。。。
achao201 2008-12-10
  • 打赏
  • 举报
回复
请问1楼的,根据我提供的xml数据,这样是不是要 new 三个 timer 。。。。
lchh0917 2008-12-10
  • 打赏
  • 举报
回复
学习~
freeboy827 2008-12-10
  • 打赏
  • 举报
回复

System.Threading.Timer tr;
tr = new System.Threading.Timer(new TimerCallback(GetData), null, 1000, 600000);
private void GetData(object o)
{
//处理数据
}
dayizhixiaotutu 2008-12-10
  • 打赏
  • 举报
回复
要是固定10秒调用一次的话1楼的久可以 根据取出的时间调用的话 一个timer以久够了
System.Threading.Timer timer;
if(timer!=null){
timer.dispose();
timer= new System.Threading.Timer(new TimerCallback(GetData), null, 0, xml文件中取出的时间);
}

上面错了
dayizhixiaotutu 2008-12-10
  • 打赏
  • 举报
回复
要是固定10秒调用一次的话1楼的久可以 根据取出的时间调用的话 一个timer以久够了
System.Threading.Timer timer;
if(timer!=null){
timer= new System.Threading.Timer(new TimerCallback(GetData), null, 0, xml文件中取出的时间);
}
wzq6511 2008-12-10
  • 打赏
  • 举报
回复
用线程去做
ericllzh 2008-12-10
  • 打赏
  • 举报
回复
学习了
dayizhixiaotutu 2008-12-10
  • 打赏
  • 举报
回复
lz是想根据xml文件取出的值 然后在确定过多长时间在调用一次吗?
achao201 2008-12-10
  • 打赏
  • 举报
回复
汗,怎么没有人回答啊。。。问题远远没有一楼的那么简单啊。。。。

111,131

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧