-
C#教程之c#一个定时重启的小程序实现代码
原理我像大家都很清楚,这里只说.NET(c#实现)。服务器每次启动或者重启,都是没登陆的。那意味着启动项里不只能做,我就考虑到使用Windows服务。Windows服务在系统加载时候就会启动所有服务,把程序写进里面应该没错了。
需要重启,那就要一个时间来效验,写法都有很多。我这里采用ini来存放。
Time 记录要重启的时间,验证时间。
Day 用来累加天数。
[Reboot]
Time=02:02:01
Day=3
使用System.Timers.Timer循环验证时间(不知道始终对占用资源是否会太大)
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") == retime)
{
Shoutdown.DoExitWin(0x00000002);
}
}
服务首次启动初始化重启的时间以及天数,初始化时候读取ini文件,把时间放进变量里,将天数累加3天。(减少读写I/O的开销)
始终不停的循环验证。
protected override void OnStart(string[] args)
{
ini = new IniFile(AppDomain.CurrentDomain.BaseDirectory + "Current.ini");
time = ini.IniReadValue("Reboot", "Time");
day = ini.IniReadValue("Reboot", "Day");
retime = DateTime.Now.AddDays(double.Parse(day)).ToString("yyyy-MM-dd ") + time;
autoTimer.AutoReset = true;
autoTimer.Enabled = true;
autoTimer.Elapsed += Timer_Elapsed;
autoTimer.Start();
}
这里可以改造成用线程来做,我想应该比这个会好。
放出一段自动备份SQLSERVER的代码端,将后会发布。
private readonly Dictionary<string, bool> cache =
new Dictionary<string, bool>();
private readonly IniFile ini = new IniFile(SysUtil.GetMapPath("Task.ini"));
private bool _flag;
private string day;
private string fHour;
private string fWeek;
private string hour;
private Thread timeThread;
protected override void OnStart(string[] args)
{
Go();
}
protected override void OnStop()
{
StopThread();
}
/// <summary>
/// 执行
/// </summary>
public void Go()
{
day = DateTime.Now.ToString("yyyy-MM-dd");
fWeek = ini.IniReadValue("Task", "Week");
fHour = ini.IniReadValue("Task", "Hour");
RunThread();
}
/// <summary>
/// 执行线程
/// </summary>
private void RunThread()
{
//这里开始线程
_flag = true;
timeThread = new Thread(ThreadTimer);
timeThread.Start();
}
/// <summary>
/// 停止线程
/// </summary>
private void StopThread()
{
_flag = false;
if (timeThread != null) timeThread.Abort();
}
/// <summary>
/// 线程方法
/// </summary>
public void ThreadTimer()
{
while (_flag)
{
if (fWeek == "") //按天
{
RunBackup();
}
else
{
// 按周
if (WeekSwitch(fWeek) == DateUtil.getWeek(DateTime.Now) && WeekSwitch(fWeek) != "")
{
RunBackup();
}
}
}
}
/// <summary>
/// 数据库列表
/// </summary>
private DataTable GetTaskList()
{
DataSet ds = new DataSet();
ds.ReadXml(SysUtil.GetMapPath("Db.xml"));
return ds.Tables["DataSet"];
}
private void RunBackup()
{
if (!File.Exists(SysUtil.GetMapPath("Db.xml"))) return;
day = DateTime.Now.ToString("yyyy-MM-dd");
hour = DateTime.Now.ToString("HH");
if (hour.Equals(fHour))
{
if (!cache.ContainsKey(day)) cache.Add(day, false);
if (fWeek != "" && cache[day] == false)
{
if (WeekSwitch(fWeek) == DateUtil.getWeek(DateTime.Parse(day)))
{
StartWork();
cache[day] = true;
}
}
else
{
if (cache[day] == false)
{
StartWork();
cache[day] = true;
}
}
private readonly Dictionary<string, bool> cache =
new Dictionary<string, bool>();
private readonly IniFile ini = new IniFile(SysUtil.GetMapPath("Task.ini"));
private bool _flag;
private string day;
private string fHour;
private string fWeek;
private string hour;
private Thread timeThread;
protected override void OnStart(string[] args)
{
Go();
}
protected override void OnStop()
{
StopThread();
}
/// <summary>
/// 执行
/// </summary>
public void Go()
{
day = DateTime.Now.ToString("yyyy-MM-dd");
fWeek = ini.IniReadValue("Task", "Week");
fHour = ini.IniReadValue("Task", "Hour");
RunThread();
}
/// <summary>
/// 执行线程
/// </summary>
private void RunThread()
{
//这里开始线程
_flag = true;
timeThread = new Thread(ThreadTimer);
timeThread.Priority = ThreadPriority.Lowest;//设置线程优先级降低系统资源占用
timeThread.Start();
}
/// <summary>
/// 停止线程
/// </summary>
private void StopThread()
{
_flag = false;
if (timeThread != null) timeThread.Abort();
}
/// <summary>
/// 线程方法
/// </summary>
public void ThreadTimer()
{
while (_flag)
{
Thread.Sleep(1000);//线程阻塞,这样可以降低系统资源占用
if (fWeek == "") //按天
{
RunBackup();
}
else
{
// 按周
if (WeekSwitch(fWeek) == DateUtil.getWeek(DateTime.Now) && WeekSwitch(fWeek) != "")
{
RunBackup();
}
}
}
}
/// <summary>
/// 数据库列表
/// </summary>
private DataTable GetTaskList()
{
DataSet ds = new DataSet();
ds.ReadXml(SysUtil.GetMapPath("Db.xml"));
return ds.Tables["DataSet"];
}
private void RunBackup()
{
day = DateTime.Now.ToString("yyyy-MM-dd");
hour = DateTime.Now.ToString("HH");
if (hour.Equals(fHour))
{
if (!cache.ContainsKey(day)) cache.Add(day, false);
if (fWeek != "" && cache[day] == false)
{
if (WeekSwitch(fWeek) == DateUtil.getWeek(DateTime.Parse(day)))
{
Console.Write("Execute");
//if (!File.Exists(SysUtil.GetMapPath("Db.xml"))) return;
//StartWork();
//cache[day] = true;
}
}
else
{
if (cache[day] == false)
{
Console.Write("Execute");
//if (!File.Exists(SysUtil.GetMapPath("Db.xml"))) return;
//StartWork();
//cache[day] = true;
}
}
}
}
到这里基本的任务已经完成,代码没有适当的优化。还有一个不完善的GUI管理工具。
需要重启,那就要一个时间来效验,写法都有很多。我这里采用ini来存放。
Time 记录要重启的时间,验证时间。
Day 用来累加天数。
复制代码 代码如下:
[Reboot]
Time=02:02:01
Day=3
使用System.Timers.Timer循环验证时间(不知道始终对占用资源是否会太大)
复制代码 代码如下:
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") == retime)
{
Shoutdown.DoExitWin(0x00000002);
}
}
服务首次启动初始化重启的时间以及天数,初始化时候读取ini文件,把时间放进变量里,将天数累加3天。(减少读写I/O的开销)
始终不停的循环验证。
复制代码 代码如下:
protected override void OnStart(string[] args)
{
ini = new IniFile(AppDomain.CurrentDomain.BaseDirectory + "Current.ini");
time = ini.IniReadValue("Reboot", "Time");
day = ini.IniReadValue("Reboot", "Day");
retime = DateTime.Now.AddDays(double.Parse(day)).ToString("yyyy-MM-dd ") + time;
autoTimer.AutoReset = true;
autoTimer.Enabled = true;
autoTimer.Elapsed += Timer_Elapsed;
autoTimer.Start();
}
这里可以改造成用线程来做,我想应该比这个会好。
放出一段自动备份SQLSERVER的代码端,将后会发布。
复制代码 代码如下:
private readonly Dictionary<string, bool> cache =
new Dictionary<string, bool>();
private readonly IniFile ini = new IniFile(SysUtil.GetMapPath("Task.ini"));
private bool _flag;
private string day;
private string fHour;
private string fWeek;
private string hour;
private Thread timeThread;
protected override void OnStart(string[] args)
{
Go();
}
protected override void OnStop()
{
StopThread();
}
/// <summary>
/// 执行
/// </summary>
public void Go()
{
day = DateTime.Now.ToString("yyyy-MM-dd");
fWeek = ini.IniReadValue("Task", "Week");
fHour = ini.IniReadValue("Task", "Hour");
RunThread();
}
/// <summary>
/// 执行线程
/// </summary>
private void RunThread()
{
//这里开始线程
_flag = true;
timeThread = new Thread(ThreadTimer);
timeThread.Start();
}
/// <summary>
/// 停止线程
/// </summary>
private void StopThread()
{
_flag = false;
if (timeThread != null) timeThread.Abort();
}
/// <summary>
/// 线程方法
/// </summary>
public void ThreadTimer()
{
while (_flag)
{
if (fWeek == "") //按天
{
RunBackup();
}
else
{
// 按周
if (WeekSwitch(fWeek) == DateUtil.getWeek(DateTime.Now) && WeekSwitch(fWeek) != "")
{
RunBackup();
}
}
}
}
/// <summary>
/// 数据库列表
/// </summary>
private DataTable GetTaskList()
{
DataSet ds = new DataSet();
ds.ReadXml(SysUtil.GetMapPath("Db.xml"));
return ds.Tables["DataSet"];
}
private void RunBackup()
{
if (!File.Exists(SysUtil.GetMapPath("Db.xml"))) return;
day = DateTime.Now.ToString("yyyy-MM-dd");
hour = DateTime.Now.ToString("HH");
if (hour.Equals(fHour))
{
if (!cache.ContainsKey(day)) cache.Add(day, false);
if (fWeek != "" && cache[day] == false)
{
if (WeekSwitch(fWeek) == DateUtil.getWeek(DateTime.Parse(day)))
{
StartWork();
cache[day] = true;
}
}
else
{
if (cache[day] == false)
{
StartWork();
cache[day] = true;
}
}
}
Code
复制代码 代码如下:
private readonly Dictionary<string, bool> cache =
new Dictionary<string, bool>();
private readonly IniFile ini = new IniFile(SysUtil.GetMapPath("Task.ini"));
private bool _flag;
private string day;
private string fHour;
private string fWeek;
private string hour;
private Thread timeThread;
protected override void OnStart(string[] args)
{
Go();
}
protected override void OnStop()
{
StopThread();
}
/// <summary>
/// 执行
/// </summary>
public void Go()
{
day = DateTime.Now.ToString("yyyy-MM-dd");
fWeek = ini.IniReadValue("Task", "Week");
fHour = ini.IniReadValue("Task", "Hour");
RunThread();
}
/// <summary>
/// 执行线程
/// </summary>
private void RunThread()
{
//这里开始线程
_flag = true;
timeThread = new Thread(ThreadTimer);
timeThread.Priority = ThreadPriority.Lowest;//设置线程优先级降低系统资源占用
timeThread.Start();
}
/// <summary>
/// 停止线程
/// </summary>
private void StopThread()
{
_flag = false;
if (timeThread != null) timeThread.Abort();
}
/// <summary>
/// 线程方法
/// </summary>
public void ThreadTimer()
{
while (_flag)
{
Thread.Sleep(1000);//线程阻塞,这样可以降低系统资源占用
if (fWeek == "") //按天
{
RunBackup();
}
else
{
// 按周
if (WeekSwitch(fWeek) == DateUtil.getWeek(DateTime.Now) && WeekSwitch(fWeek) != "")
{
RunBackup();
}
}
}
}
/// <summary>
/// 数据库列表
/// </summary>
private DataTable GetTaskList()
{
DataSet ds = new DataSet();
ds.ReadXml(SysUtil.GetMapPath("Db.xml"));
return ds.Tables["DataSet"];
}
private void RunBackup()
{
day = DateTime.Now.ToString("yyyy-MM-dd");
hour = DateTime.Now.ToString("HH");
if (hour.Equals(fHour))
{
if (!cache.ContainsKey(day)) cache.Add(day, false);
if (fWeek != "" && cache[day] == false)
{
if (WeekSwitch(fWeek) == DateUtil.getWeek(DateTime.Parse(day)))
{
Console.Write("Execute");
//if (!File.Exists(SysUtil.GetMapPath("Db.xml"))) return;
//StartWork();
//cache[day] = true;
}
}
else
{
if (cache[day] == false)
{
Console.Write("Execute");
//if (!File.Exists(SysUtil.GetMapPath("Db.xml"))) return;
//StartWork();
//cache[day] = true;
}
}
}
}
到这里基本的任务已经完成,代码没有适当的优化。还有一个不完善的GUI管理工具。
最新更新
Objective-C语法之代码块(block)的使用
VB.NET eBook
Add-in and Automation Development In VB.NET 2003 (F
Add-in and Automation Development In VB.NET 2003 (8
Add-in and Automation Development in VB.NET 2003 (6
Add-in and Automation Development In VB.NET 2003 (5
AddIn Automation Development In VB.NET 2003 (4)
AddIn And Automation Development In VB.NET 2003 (2)
Addin and Automation Development In VB.NET 2003 (3)
AddIn And Automation Development In VB.NET 2003 (1)
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
武装你的WEBAPI-OData入门
武装你的WEBAPI-OData便捷查询
武装你的WEBAPI-OData分页查询
武装你的WEBAPI-OData资源更新Delta
5. 武装你的WEBAPI-OData使用Endpoint 05-09
武装你的WEBAPI-OData之API版本管理
武装你的WEBAPI-OData常见问题
武装你的WEBAPI-OData聚合查询
OData WebAPI实践-OData与EDM
OData WebAPI实践-Non-EDM模式