private static DateTime ThisStartTime = DateTime.Now; //本次啟動(dòng)時(shí)間
private static string AutoStartTime1 { get; set; } //自動(dòng)重啟時(shí)間1
private static string AutoStartTime2 { get; set; } //自動(dòng)重啟時(shí)間2
private static string AutoStartTime3 { get; set; } //自動(dòng)重啟時(shí)間3
/*
此處自行補(bǔ)充獲取AutoStartTime1、AutoStartTime2、AutoStartTime3三個(gè)值的代碼
*/
private void timerResetAPP_Tick(object sender, EventArgs e)
{
//判斷是否需要自動(dòng)重啟本程序
if (AutoStartTime1 != "" || AutoStartTime2 != "" || AutoStartTime3 != "")
{
DateTime temp_time;
string thisDay = DateTime.Now.ToString("yyyy-MM-dd");
TimeSpan ts_check_auto_start_1;
TimeSpan ts_check_auto_start_2;
int auto_start_flag = 0;
if (AutoStartTime1 != "" && auto_start_flag == 0)
{
temp_time = Convert.ToDateTime($"{thisDay} {AutoStartTime1}:00");
ts_check_auto_start_1 = DateTime.Now - temp_time;
ts_check_auto_start_2 = temp_time - ThisStartTime;
if (ts_check_auto_start_1.TotalSeconds > 0 && ts_check_auto_start_2.TotalSeconds > 0) { auto_start_flag = 1; }
}
if (AutoStartTime2 != "" && auto_start_flag == 0)
{
temp_time = Convert.ToDateTime($"{thisDay} {AutoStartTime2}:00");
ts_check_auto_start_1 = DateTime.Now - temp_time;
ts_check_auto_start_2 = temp_time - ThisStartTime;
if (ts_check_auto_start_1.TotalSeconds > 0 && ts_check_auto_start_2.TotalSeconds > 0) { auto_start_flag = 1; }
}
if (AutoStartTime3 != "" && auto_start_flag == 0)
{
temp_time = Convert.ToDateTime($"{thisDay} {AutoStartTime3}:00");
ts_check_auto_start_1 = DateTime.Now - temp_time;
ts_check_auto_start_2 = temp_time - ThisStartTime;
if (ts_check_auto_start_1.TotalSeconds > 0 && ts_check_auto_start_2.TotalSeconds > 0) { auto_start_flag = 1; }
}
//自動(dòng)重啟本程序
if (auto_start_flag == 1) { resetAPP(); }
}
}
private void resetAPP()
{
//先隱藏本程序右下角圖標(biāo)(如果有右下角小圖標(biāo),則需要加上這個(gè)隱藏小圖標(biāo)命令,否則不需要下面這行)
notifyIcon.Visible = false;
//獲取并啟動(dòng)當(dāng)前進(jìn)程的可執(zhí)行文件的完整路徑
StartProcess(Application.ExecutablePath);
//徹底退出
Thread.Sleep(100); //為確保正確執(zhí)行,延遲100毫秒退出
Process.GetCurrentProcess().Kill();
Environment.Exit(0);
}