c sharp a scheduled restart of the small program implementation code

  • 2020-05-05 11:47:40
  • OfStack

I think the principle is pretty clear to you, just say.NET (c# implementation). Every time the server is started or restarted, it is not logged in. That means that not only can I do this in the startup, I'm thinking about using the Windows service. The Windows service starts all the services when the system loads, so it should be fine to write programs into it.
Need to restart, that will take a time to effect, there are many ways to write. I use ini for storage here.
Time records the time to restart and the verification time.
Day is used to add up days.

[Reboot] 
Time=02:02:01 
Day=3 


Loop validation time with System.Timers.Timer (not sure if it will be too big for the resource to be used all the time)

private void Timer_Elapsed(object sender, ElapsedEventArgs e) 
{ 
if (DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") == retime) 
{ 
Shoutdown.DoExitWin(0x00000002); 
} 
} 


The service first starts, initializes the restart time, and the number of days, reads the ini file at initialization, puts the time into a variable, and adds up the number of days to 3 days. (reduce the overhead of reading and writing I/O)
Always loop through validation.

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(); 
} 


I think it's better to do it with threads than this.

An automatic backup SQLSERVER code side will be released later.

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> 
///  perform  
/// </summary> 
public void Go() 
{ 
day = DateTime.Now.ToString("yyyy-MM-dd"); 
fWeek = ini.IniReadValue("Task", "Week"); 
fHour = ini.IniReadValue("Task", "Hour"); 
RunThread(); 
} 

/// <summary> 
///  The thread of execution  
/// </summary> 
private void RunThread() 
{ 
// So this is where the thread starts  
_flag = true; 
timeThread = new Thread(ThreadTimer); 
timeThread.Start(); 
} 

/// <summary> 
///  Stop the thread  
/// </summary> 
private void StopThread() 
{ 
_flag = false; 
if (timeThread != null) timeThread.Abort(); 
} 

/// <summary> 
///  Thread method  
/// </summary> 
public void ThreadTimer() 
{ 
while (_flag) 
{ 
if (fWeek == "") // According to the days  
{ 
RunBackup(); 
} 
else 
{ 
//  According to the week  
if (WeekSwitch(fWeek) == DateUtil.getWeek(DateTime.Now) && WeekSwitch(fWeek) != "") 
{ 
RunBackup(); 
} 
} 
} 
} 

/// <summary> 
///  Database list  
/// </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> 
///  perform  
/// </summary> 
public void Go() 
{ 
day = DateTime.Now.ToString("yyyy-MM-dd"); 
fWeek = ini.IniReadValue("Task", "Week"); 
fHour = ini.IniReadValue("Task", "Hour"); 
RunThread(); 
} 

/// <summary> 
///  The thread of execution  
/// </summary> 
private void RunThread() 
{ 
// So this is where the thread starts  
_flag = true; 
timeThread = new Thread(ThreadTimer); 
timeThread.Priority = ThreadPriority.Lowest;// Set thread priority to reduce system resource usage  
timeThread.Start(); 
} 

/// <summary> 
///  Stop the thread  
/// </summary> 
private void StopThread() 
{ 
_flag = false; 
if (timeThread != null) timeThread.Abort(); 
} 

/// <summary> 
///  Thread method  
/// </summary> 
public void ThreadTimer() 
{ 
while (_flag) 
{ 
Thread.Sleep(1000);// Thread blocking to reduce system resource usage  
if (fWeek == "") // According to the days  
{ 
RunBackup(); 
} 
else 
{ 
//  According to the week  
if (WeekSwitch(fWeek) == DateUtil.getWeek(DateTime.Now) && WeekSwitch(fWeek) != "") 
{ 
RunBackup(); 
} 
} 
} 
} 

/// <summary> 
///  Database list  
/// </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; 
} 
} 
} 
} 

At this point the basic task is done and the code is not properly optimized. There is also an imperfect GUI management tool.  
  http://xiazai.jb51.net/tools/system/Reboot(c#).rar

Related articles: