Windows服务每天只运行12.00 [英] Windows services run only 12.00 a.m every day

查看:102
本文介绍了Windows服务每天只运行12.00的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



这是Ramesh,



我的要求是仅运行Windows服务每天上午12点(即午夜)..但我必须写一些代码但不能正常工作。



请一次通过以下代码作为

Hi All,

This is Ramesh,

My Requirement is "Windows services run only 12.00 a.m every day(i.e midnight)"..But i have to write some Code but not working.

Please once go through the following code as

static void my_task(Object obj)
       {
           Console.WriteLine("task being performed.");
       }

       static TimerCallback timerDelegate;
       static Timer timer;

       static void Main(string[] args)
       {
           DateTime now = DateTime.Now;
           DateTime today = now.Date.AddHours(0);
           DateTime next = now <= today ? today : today.AddDays(1);

           timerDelegate = new TimerCallback(my_task);

           // hence the first,after the next
           timer = new Timer(timerDelegate, null, next - DateTime.Now, TimeSpan.FromHours(24));
       }



我知道如何设置时间间隔,即小时明智,薄荷明智......等待...当我搜索时 Google它表明,Schedule Tasks更适合所有网站,论坛......而不是Windows服务但是我的经理不同意我...你能否给我一些宝贵的建议。


$ b $bThanks®ards,



Ramesh.n


I know how to set up the Time intervals i.e Hrs wise,Mint's wise..Etc...When i am searching "Google" it shows that Schedule Tasks is better option all sites,forums..etc instead of Windows Services But My Managers not agree with me...Could you please give me valuable suggestion.

Thanks®ards,

Ramesh.n

推荐答案

Windows服务旨在一直工作,直到明确停止或直到系统关闭。仅使用您的计时器启动某项工作服务的某些操作。



但是您可能根本不需要自己的服务?如果您只需要在特定时刻启动某些操作,则可以使用专门为此目的设计的现有服务,该服务可以支持非常复杂的计划。它已与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 services are designed to work all the time until explicitly stopped or until system shutdown. Use your timer only to initiate some action of a working service.

But maybe you don't need your own service at all? If you only need to initiate some action at certain moments of time, you can use already available service, designed specially for this purpose, which can 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


这篇关于Windows服务每天只运行12.00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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