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調度程序進行計劃 –