一个使用C#和ArcPy实现的版本压缩工具(四)
目录
目录
定时任务的执行
private Thread timeThread = null; //定时任务线程
private bool StartMission()
{
var b = false;
try
{
int.TryParse(CompressionParameter.m_dictConfig[CompressionParameter.m_sHour], out int nHour);
int.TryParse(CompressionParameter.m_dictConfig[CompressionParameter.m_sMinute], out int nMinute);
int.TryParse(CompressionParameter.m_dictConfig[CompressionParameter.m_sCompressionMode], out int nMode);
int.TryParse(CompressionParameter.m_dictConfig[CompressionParameter.m_sRepeat], out int nRepeat);
if (null != timeThread)
{
timeThread.Abort();
}
timeThread = new Thread(new ThreadStart(delegate
{
EventTask(nHour, nMinute, nMode, nRepeat == 1);
}))
{
IsBackground = true
};
timeThread.Start();
b = true;
}
catch (Exception e)
{
Log.Loging.Error(e.Message);
}
return b;
}
private void EventTask(int nHour, int nMinute, int nMode, bool bRepeat)
{
Log.Loging.Info($"Event Task Time -- {nHour}:{nMinute}");
while (true)
{
DateTime dateTimeNow = DateTime.Now;
Log.Loging.Info($"Check Time -- {dateTimeNow.Hour}:{dateTimeNow.Minute}:{dateTimeNow.Second}");
if ((0 == dateTimeNow.Hour - nHour) && (0 == dateTimeNow.Minute - nMinute))
{
switch (nMode)
{
case 0:
CompressNormally();
break;
case 1:
CompressAutomatically();
break;
case 2:
CompressCompletely();
break;
default:
break;
}
Thread.Sleep(60000); //完成一次压缩后休息一分钟,避免频繁压缩
}
if (!bRepeat)
break;
Thread.Sleep(20000); //每隔20秒判断一下当前时间
}
}
开机启动
private void CheckBoxAutoRun_CheckedChanged(object sender, EventArgs e)
{
try
{
string sPath = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(CompressionParameter.m_sRegistryNode, RegistryKeyPermissionCheck.ReadWriteSubTree);
if (checkBoxAutoRun.Checked)
{
rk2.SetValue(CompressionParameter.m_sRegistryKey, sPath);
}
else
{
rk2.DeleteValue(CompressionParameter.m_sRegistryKey, false);
}
rk2.Close();
rk.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
Log.Loging.Error(ex.Message);
}
}
转载自:https://blog.csdn.net/a_dev/article/details/88600298