如何安排窗口服务? [英] How to schedule window service ?

查看:60
本文介绍了如何安排窗口服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们创建了一个web服务,根据时间...我们在webconfig中传递参考下面的代码

 <   add     key     = 模式    value     = 每日  /  >  
<! - < add key =模式值=间隔/> - >
< add key = IntervalMinutes value = 1 / >
< add key = ScheduledTime = 18: 30 / >







在Onstart事件中,我创建了将更新数据库的方法

public void ScheduleService()

{

尝试

{

Schedular = new Timer(新的TimerCallback(SchedularCallback));

string mode = ConfigurationManager。 AppSettings [Mode]。ToUpper();

this.WriteToFile(简单服务模式:+模式+{0});



//设置默认时间。

DateTime scheduledTime = DateTime.MinValue;



if(mode ==DAILY )

{

//从AppSettings获取预定时间。

scheduledTime = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings [ ScheduledTime]);

if(DateTime.Now>预定时间)

{

this.pulldata();

//如果通过预定时间,则设定第二天的时间表。

scheduledTime = scheduledTime.AddDays(1);

}

else

{

if (DateTime.Now == scheduledTime)

{

this.pulldata();

//如果通过预定时间,则为第二天。

scheduledTime = scheduledTime.AddDays(1);

}

}

}



if(mode.ToUpper()==INTERVAL)

{

//在几分钟内获取间隔来自AppSettings。
int intervalMinutes = Convert.ToInt32(ConfigurationManager.AppSettings [IntervalMinutes]);



//通过添加间隔来设置预定时间当前时间。

scheduledTime = DateTime.Now.AddMinutes(intervalMinutes);

if(DateTime.Now> scheduledTime)

{

//如果超过预定时间,则为下一个时间间隔设置时间表。

scheduledTime = scheduledTime.AddMinutes(intervalMinutes);

}

}



TimeSpan timeSpan = scheduledTime.Subtract(DateTime.Now);

string schedule = string.Format({0} day(s){1}小时{2}分钟{3}秒(s),timeSpan.Days,timeSpan.Hours, timeSpan.Minutes,timeSpan.Seconds);



this.WriteToFile(简单服务计划在之后运行:+ schedule +{0});



//获取预定时间和当前时间之间的分钟数。

int dueTime = Convert.ToInt32(timeSpan.TotalMilliseconds);



//更改计时器的截止时间。

Schedular.Change(dueTime,Timeout.Infinite);

}

catch(例外情况)

{

WriteToFile(简单服务错误:{0}+ ex.Message + ex.StackTrace);



//停止Windows服务。

using(System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController(SimpleService))

{

serviceController.Stop();

}

}

}





之后构建我在动作按钮中传递任务计划中的exe,我收到错误事件103 ..



如何解决此错误。



任何帮助

解决方案

看起来你没有尝试安排服务(比如,启动,停止等等) ),但是chedule由服务执行的一些动作。 这是一个非常好的主意。这些服务旨在永久执行,可能设置为等待状态的大部分运行时。这样,服务就是一种调度某些事件的写方式。



但是有两个问题。 首先,不清楚你的问题是什么(我会说,这不是一个问题);并且但它没有运行没有提供信息。



其次,您可能不需要创建服务,因为您可能需要使用现有的服务。难道你不认为你最好使用旨在支持真正复杂的时间表的现有服务吗?它已与Windows捆绑并启用;你可以在不同的层面上使用它。它被称为Window Task Scheduler,请参阅 http://en.wikipedia.org/wiki/Windows_Task_Scheduler [ ^ ]。



首先,您可以使用命令行实用程序AT.EXE或CSHTASKS.EXE(替换AT.EXE)来安排事件,请参阅:

http://en.wikipedia.org/wiki/At_%28Windows%29 [ ^ ],

http://en.wikipedia.org/wiki/Schtasks [ ^ ],

http://technet.microsoft .com / zh-cn / library / bb490866.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357%28v=vs.85%29 .aspx [ ^ ]。



您还可以使用Window Task Scheduler API,请参阅:

http://msdn.microsoft.com/en-我们/ library / windows / desktop / aa383614%28v = vs.85%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/windows/desktop/aa383608%28v= vs.85%29.aspx [ ^ ] 。



要了解如何在.NET中使用它,请参阅此CodeProject文章:用于.NET的新任务计划程序类库 [ ^ ]。



-SA


你不要安排Windows服务停止开始,你编写一个服务,使它不断运行,如果服务是以一定间隔运行其他任务,那么你就让服务休眠,在有事情要做的时候醒来。因此,你可以让它睡一个小时,当它醒来时,它做了需要做的事情。或者你可以让它睡5分钟,每次醒来都会看到它是否有工作要做,如果有的话。



如果你google写作调度服务c#然后你会找到你需要的代码的示例模板,但简而言之,服务有一个主线程,你用来响应停止事件,当主线程启动时,它将启动第二个线程,它做实际的工作,第二个线程将是定期休眠的一个线程,理想情况下使用ManualResetEvent,这样主线程也可以在服务被告知停止的情况下唤醒辅助线程。



就像我说的那样,谷歌你会找到示例代码。



http://www.c-sharpcorner.com/UploadFile/ ee01e6 / create-windows-service-and-send-mail-daily-on-fixed-time-usi575 / [ ^ ] < br $>


创建Scehduler服务使用Windows服务 [ ^ ]

We have created a webservice which will according to the time .. which we pass in webconfig refer to code below

<add key ="Mode" value ="Daily"/>
   <!-- <add key ="Mode" value ="Interval"/>-->
   <add key ="IntervalMinutes" value ="1"/>
   <add key ="ScheduledTime" value ="18:30"/>




On the Onstart event i have created method which will update the database
public void ScheduleService()
{
try
{
Schedular = new Timer(new TimerCallback(SchedularCallback));
string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();
this.WriteToFile("Simple Service Mode: " + mode + " {0}");

//Set the Default Time.
DateTime scheduledTime = DateTime.MinValue;

if (mode == "DAILY")
{
//Get the Scheduled Time from AppSettings.
scheduledTime = DateTime.Parse(System.Configuration.ConfigurationManager.AppSettings["ScheduledTime"]);
if (DateTime.Now > scheduledTime)
{
this.pulldata();
//If Scheduled Time is passed set Schedule for the next day.
scheduledTime = scheduledTime.AddDays(1);
}
else
{
if (DateTime.Now == scheduledTime)
{
this.pulldata();
//If Scheduled Time is passed set Schedule for the next day.
scheduledTime = scheduledTime.AddDays(1);
}
}
}

if (mode.ToUpper() == "INTERVAL")
{
//Get the Interval in Minutes from AppSettings.
int intervalMinutes = Convert.ToInt32(ConfigurationManager.AppSettings["IntervalMinutes"]);

//Set the Scheduled Time by adding the Interval to Current Time.
scheduledTime = DateTime.Now.AddMinutes(intervalMinutes);
if (DateTime.Now > scheduledTime)
{
//If Scheduled Time is passed set Schedule for the next Interval.
scheduledTime = scheduledTime.AddMinutes(intervalMinutes);
}
}

TimeSpan timeSpan = scheduledTime.Subtract(DateTime.Now);
string schedule = string.Format("{0} day(s) {1} hour(s) {2} minute(s) {3} seconds(s)", timeSpan.Days, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);

this.WriteToFile("Simple Service scheduled to run after: " + schedule + " {0}");

//Get the difference in Minutes between the Scheduled and Current Time.
int dueTime = Convert.ToInt32(timeSpan.TotalMilliseconds);

//Change the Timer's Due Time.
Schedular.Change(dueTime, Timeout.Infinite);
}
catch (Exception ex)
{
WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);

//Stop the Windows Service.
using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
{
serviceController.Stop();
}
}
}


after the build I m passing the exe in task schedular in the action button ,i received the error Event 103 ..

how to solve this error.

any help

解决方案

It looks like you are not trying to schedule a service (say, starting, stopping, and so on), but schedule some action performed by the service. And this is quite a good idea. The services are designed to be executed permanently, possibly set to wait state big part of their runtime. This way, a service is a write way of scheduling of some events.

There are two problems though. First, it's not clear what is your question (I would say, this is not a question); and "but it is not running" is not informative.

Secondly, you might not need to create a service, because you may want to use already existing service. Don't you think you would better off using existing service which is designed to support really complex schedules? It is already bundled with Windows and enabled; you can use it on different levels. It is called Window Task Scheduler, see http://en.wikipedia.org/wiki/Windows_Task_Scheduler[^].

First, you can schedule events using command-line utilities AT.EXE or CSHTASKS.EXE (which is replacing AT.EXE), see:
http://en.wikipedia.org/wiki/At_%28Windows%29[^],
http://en.wikipedia.org/wiki/Schtasks[^],
http://technet.microsoft.com/en-us/library/bb490866.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357%28v=vs.85%29.aspx[^].

And you also can use Window Task Scheduler API, please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa383614%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa383608%28v=vs.85%29.aspx[^].

To see how can you use it with .NET, see this CodeProject article: A New Task Scheduler Class Library for .NET[^].

—SA


You don't schedule Windows Services to stop\start, you write a service such that it runs constantly and if the service is to run other tasks at an interval then you make the service sleep, waking up when it has something to do. So you can make it sleep for an hour and when it wakes up it does what needs to be done. Or you can make it sleep for 5 minutes and every time it wakes it sees if it has work to do, and if so does it.

If you google "writing scheduling service c#" then you'll find example templates of the code you need, but in a nutshell the service has a main thread that you use to respond to stop events, and when the main thread starts it will start a second thread that does the actual work, and this second thread will be the one that periodically sleeps\wakes up, ideally using a ManualResetEvent so that the main thread can also wake the secondary thread in the event that the service is told to stop.

Like I said, google and you'll find example code.

http://www.c-sharpcorner.com/UploadFile/ee01e6/create-windows-service-and-send-mail-daily-on-fixed-time-usi575/[^]

Creating Scehduler Service using Windows Services[^]


这篇关于如何安排窗口服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆