服务程序(Service1.cs)
protected override void OnStart(string[] args)
{
//调试用
//System.Threading.Thread.Sleep(15000);
log4net.Config.XmlConfigurator.ConfigureAndWatch( new System.IO.FileInfo(Sxmobi.FileHelper.GetMapPath("")+ "log4net.config"));
_timer = new System.Timers.Timer(_interval);
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Start();
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
//要做的事情
this._timer.Enabled = false;
try
{
while (true)
{
Sxmobi.LogHelper.Info(this.GetType().ToString(), "VP2抓取程序开始", null);
TaskProc tp = new TaskProc();
tp.LoadTask();
WaitHandle.WaitAll(tp.waitHandles);//配置文件更改后此语句会执行
}
}
catch (Exception ex)
{
Sxmobi.LogHelper.Error(this.GetType().ToString() ,ex.Message,ex);
Sxmobi.LogHelper.Info(this.GetType().ToString(), "VP2抓取程序已停止", null);
}
}
监控程序(TaskProc.cs)
public WaitHandle[] waitHandles;
public TaskProc()
{
}
public void LoadTask()
{
ListParser lp = new ListParser();
ListParser.iCurrent++;//结束上一个线程
new Thread(new ThreadStart(lp.RunBat)).Start();
JianKong();
}
void JianKong()
{
this.waitHandles = new WaitHandle[] { new AutoResetEvent(false) };
AutoResetEvent are = (AutoResetEvent)this.waitHandles[0];
do
{
Thread.Sleep(10000);
}
while (!SiteConfig.ConfigIsUpdate);//配置文件更改后执行下面语句
are.Set();
}
主程序(ListParser.cs)
class ListParser
{
public static int iCurrent = 0;
public ListParser() { }
public void RunBat()
{
int ic = iCurrent;
while (ic==iCurrent)//是否是同一线程
{
RutAllFiles(SiteConfig.BatFileRoot);
Thread.Sleep(1000);
}
}
private void RutAllFiles(string fileDirectory)
{
DirectoryInfo diSource = new DirectoryInfo(fileDirectory);
FileSystemInfo[] fsi = diSource.GetFileSystemInfos();
FileInfo fi;
try
{
for (int i = 0; i < fsi.Length; i++)
{
//不是目录,查看文件属性;是目录,继续遍历。
if (Directory.Exists(fsi[i].FullName) == false)
{
fi = new FileInfo(fsi[i].FullName);
//判断文件类型,进行相应的后继操作。
if (fi.Extension == ".bat")
{
try
{
Process p = new Process();
ProcessStartInfo psi = new ProcessStartInfo(fi.FullName);
p.StartInfo = psi;
p.Start();
p.WaitForExit(30000);
File.Delete(fi.FullName);
Thread.Sleep(500);
}
catch (Exception ex)
{
Sxmobi.LogHelper.Error(this.GetType().ToString(), ex.Message, ex);
}
}
}
else
{
RutAllFiles(fsi[i].FullName);
}
}
}
catch (Exception ex)
{
Sxmobi.LogHelper.Error(this.GetType().ToString(), ex.Message, ex);
}
}
读取配置程序(SiteConfig.cs)
private static string xmlPath = Sxmobi.FileHelper.GetMapPath("~") + "\\Config\\Site.config";
static XmlDocument xmlDoc = Sxmobi.XmlHelper.GetXmlDoc(xmlPath);
private static DateTime LastestXmlModefyTime = Utility.getFileModifyTime(xmlPath);
public static bool ConfigIsUpdate
{
get
{
try
{
if ((Utility.getFileModifyTime(xmlPath) != DateTime.MinValue) && (LastestXmlModefyTime != Utility.getFileModifyTime(xmlPath)))
{
LastestXmlModefyTime = Utility.getFileModifyTime(xmlPath);
xmlDoc = Sxmobi.XmlHelper.GetXmlDoc(xmlPath);//重新读取config
return true;
}
return false;
}
catch
{
return false;
}
}
}
public static string BatFileRoot
{
get
{
return Sxmobi.XmlHelper.GetNodeValue(xmlDoc, "/Site/BatFileRoot");
}
}