0
A
回答
2
运行总是:去为Windows服务 对于周期性的事:去定时器
所以有一个Windows服务,维护定时器设定在规定的时间间隔来触发,做任何你需要在那里。
0
这是我的例子,对于一些任务没关系。
public class Timer : IRegisteredObject
{
private Timer _timer;
public static void Start()
{
HostingEnvironment.RegisterObject(new Timer());
}
public Timer()
{
StartTimer();
}
private void StartTimer()
{
_timer = new Timer(BroadcastUptimeToClients, null, TimeSpan.FromSeconds(0), TimeSpan.FromMilliseconds(100));
}
private void BroadcastUptimeToClients(object state)
{
//some action
}
public void Stop(bool immediate)
{
//throw new System.NotImplementedException();
}
}
在Global.asax中
Timer.Start();
在你的情况我会@阿尔森Mkrtchyan同意 - 使用OS调度器。 如果您想使用Windows服务,这里是如何会看你的服务:
partial class MyService
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
private Timer _watcherTimer = new System.Timers.Timer();
private static Logger logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "MyService";
this._watcherTimer.Interval = 6000;
this._watcherTimer.Enabled = false;
this._watcherTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.Timer_Tick);
}
#endregion
}
partial class MyService : ServiceBase
{
public MyService()
{
try
{
InitializeComponent();
}
catch (Exception e)
{
logger.Error("Error initializing service",e);
Stop();
}
}
protected override void OnStart(string[] args)
{
_watcherTimer.Enabled = true;
logger.Info("Service has started at " + DateTime.UtcNow.ToLongDateString());
}
protected override void OnStop()
{
logger.Info("Service has stopped at " + DateTime.UtcNow.ToLongDateString());
}
private void Timer_Tick(object sender, ElapsedEventArgs e)
{
logger.Info("Timer Tick");
}
}
安装:
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer
{
/// <summary>
/// Public Constructor for WindowsServiceInstaller.
/// - Put all of your Initialization code here.
/// </summary>
public WindowsServiceInstaller()
{
var serviceProcessInstaller =
new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
//# Service Account Information
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
//# Service Information
serviceInstaller.DisplayName = "MY SERVICE DISPLAY NAME";
serviceInstaller.Description = "MY SERVICE DESCRIPTION";
serviceInstaller.StartType = ServiceStartMode.Automatic;
//# This must be identical to the WindowsService.ServiceBase name
//# set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = "MyService";
Installers.Add(serviceProcessInstaller);
Installers.Add(serviceInstaller);
}
}
与蝙蝠
static class Program
{
static void Main(string[] args)
{
if (args.Length > 0)
{
//Install service
if (args[0].Trim().ToLower() == "/i")
{ System.Configuration.Install.ManagedInstallerClass.InstallHelper(new[] { "/i", Assembly.GetExecutingAssembly().Location }); }
//Uninstall service
else if (args[0].Trim().ToLower() == "/u")
{ System.Configuration.Install.ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location }); }
}
else
{
var servicesToRun = new ServiceBase[] { new MyService() };
ServiceBase.Run(servicesToRun);
}
}
}
INSTALL.BAT
运行此@ECHO OFF
REM Prevent changing current directory when run bat file as administrator on win7
@setlocal enableextensions
@cd /d "%~dp0"
REM The following directory is for .NET 4.0
set DOTNETFX4=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX4%
echo Installing WindowsService...
echo "%CD%\WindowsService\Service\bin\Debug\Service.exe"
echo ---------------------------------------------------
InstallUtil /i "%CD%\WindowsService\Service\bin\Debug\Service.exe"
echo ---------------------------------------------------
echo Done.
set /p DUMMY=Hit ENTER to continue...
unistall.bat
@ECHO OFF
REM Prevent changing current directory when run bat file as administrator on win7
@setlocal enableextensions
@cd /d "%~dp0"
REM The following directory is for .NET 4.0
set DOTNETFX4=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX4%
echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /u "%CD%\WindowsService\Service\bin\Debug\Service.exe"
echo ---------------------------------------------------
echo Done.
set /p DUMMY=Hit ENTER to continue...
我的文件夹层次:
install.bat
uninstall.bat
service-project-folder
packages
project-folder
.sln file
1
您可以使用开源Quartz.NET调度(http://www.quartz-scheduler.net/),它可以在指定的时间和间隔时间触发作业。我的建议是在Windows服务中托管调度程序作业。
0
通过使用ReactiveExtensions,您可以使用以下代码,如果您有兴趣进行如打印到控制台那样简单的操作,请点击此处https://stackoverflow.com/questions/30473281/how-to-print-a-string-in-c-sharp-after-every-1-minute-using-timer。您可以通过NuGet添加Rx-Main来添加Reactive Extensions。
using System;
using System.Reactive.Linq;
namespace ConsoleApplicationExample
{
class Program
{
static void Main()
{
Observable.Interval(TimeSpan.FromMinutes(1))
.Subscribe(_ =>
{
Console.WriteLine(DateTime.Now.ToString());
});
Console.WriteLine(DateTime.Now.ToString());
Console.ReadLine();
}
}
}
相关问题
- 1. 如何在特定时间和日期运行某个功能?
- 2. 如何隐式执行特定时间的某些功能
- 3. 如何在给定时间后执行某个功能?
- 4. 在特定时间执行功能
- 5. 在特定的时间执行功能
- 6. 在特定时间运行SQL进程
- 7. 如何验证一个并行线程功能开始执行
- 8. QT,运行特定时间的功能
- 9. 如何定期执行某些功能,如果这个功能随时间需要运行(比peroid以下)
- 10. 在某个时间执行某个功能
- 11. 如何在春季的特定时间后执行功能?
- 12. 如何在Unity3d中运行进程时执行某些操作
- 13. 始终在Backbone中执行父功能
- 14. 节点是否可以在特定时间内执行某个功能?
- 15. 如何在特定时间后运行功能
- 16. 如何在执行某个其他功能时停止某个功能?
- 17. 如何在Python中的特定时间运行某个函数?
- 18. 如何在Web应用程序的特定日期执行某个功能?
- 19. 在特定时间执行特殊功能的Windows服务
- 20. 如何在一段时间后执行某些功能?
- 21. 以特定间隔执行功能
- 22. 始终使用IISNode运行进程
- 23. 在某些时钟时执行功能
- 24. 如何知道特定功能的运行时间?
- 25. 如何使用线程测量时间运行多个功能?
- 26. 始终在零时间阻止执行
- 27. 如何首先执行特定功能
- 28. 如何执行特定秒的功能
- 29. 终止长时间运行的进程
- 30. 如何在某个功能完成后才能执行某个功能?
创建一个进程,调度它与Windows调度...你试过什么?问题是什么? –
其实我还没有尝试过任何东西,因为我试图理解,并且从哪里开始 – ARC
安排某些事情的最佳方式是使用OS Scheduler applciation,不要尝试创建您的调度程序,因为OS调度程序更稳定,支持长计时器周期等等......所以你只需要编写一个进程就可以做一次,并使用Windows或Linux调度程序进行计划 –