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管理工具。
2
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论