我写了一个Windows服务,这个服务在OnStart的时候会起一个线程
这个线程用来定时启动一个外部的exe来执行一些操作
现在发现每次线程在起动外部exe时生成的handle,在exe执行完后系统不回收
昨晚连续跑了一夜程序,刚开始服务程序的handle在200左右,今早一来涨到了2400
不过内存没有问题一直稳定在9200,大家帮忙给看一下啊,谢谢了。下面是线程部分的代码:
对了,我注释掉了启动exe的部分(try-catch部分),服务程序跑起来没有问题,没有任何的溢出
#region file observe thread
/// <summary>
/// file observe
/// </summary>
/// <returns>true:success </returns>
public void FileDataReceiveThread()
{
DateTime CurrentTime = new DateTime();
DateTime LastTime = new DateTime(); // 以下是外部exe执行时需要的参数
string strWebService = ParameterArr[2];
string strDir = ParameterArr[3];
string strTerm = ParameterArr[4];
int intTerm = Int32.Parse(strTerm);
string strFileDataReceive = ParameterArr[5]; string strLine = strWebService + " " + strDir;
for (int i = 6; i < ParameterArr.Length; i++)
{
strLine = strLine + ParameterArr[i];
} // sleep
Thread.Sleep(intTerm * 1000); // loop
while (true)
{
// 服务OnStop时,停止线程
Boolean bSuspendEventChanged = SuspendChangedEvent.WaitOne(0, true);
if (bSuspendEventChanged)
{
break;
} CurrentTime = DateTime.Now;
TimeSpan tsCurrentTime = new TimeSpan(CurrentTime.Ticks);
TimeSpan tsLastTime = new TimeSpan(LastTime.Ticks);
TimeSpan tsMinus = tsCurrentTime.Subtract(tsLastTime).Duration(); string strSecondsMinus = tsMinus.Seconds.ToString();
int intSecondsMinus = Int32.Parse(strSecondsMinus); if (intSecondsMinus < intTerm)
{
Thread.Sleep(1);
continue;
} try
{
Process proc = new Process();
proc.StartInfo.FileName = strFileDataReceive;
proc.StartInfo.Arguments = strLine;
proc.EnableRaisingEvents = true;
proc.Start();
if (proc == null)
{
Console.WriteLine(Message.PROC_RUNING_ERR);
EventLog.WriteEntry(Message.APP_NAME, Message.PROC_RUNING_ERR,
EventLogEntryType.Information, ConstString.EVENTID, ConstString.CATEGORY); return;
}
else
{
proc.WaitForExit(); proc.Dispose();
if (proc != null)
{
proc.Close();
}
proc = null;
}
}
catch (System.Exception e)
{
// err log
Console.WriteLine(e.ToString());
EventLog.WriteEntry(Message.APP_NAME, e.ToString(),
EventLogEntryType.Information, ConstString.EVENTID, ConstString.CATEGORY);
return;
} LastTime = DateTime.Now;
}
}
#endregion ファイル監視スレッド
这个线程用来定时启动一个外部的exe来执行一些操作
现在发现每次线程在起动外部exe时生成的handle,在exe执行完后系统不回收
昨晚连续跑了一夜程序,刚开始服务程序的handle在200左右,今早一来涨到了2400
不过内存没有问题一直稳定在9200,大家帮忙给看一下啊,谢谢了。下面是线程部分的代码:
对了,我注释掉了启动exe的部分(try-catch部分),服务程序跑起来没有问题,没有任何的溢出
#region file observe thread
/// <summary>
/// file observe
/// </summary>
/// <returns>true:success </returns>
public void FileDataReceiveThread()
{
DateTime CurrentTime = new DateTime();
DateTime LastTime = new DateTime(); // 以下是外部exe执行时需要的参数
string strWebService = ParameterArr[2];
string strDir = ParameterArr[3];
string strTerm = ParameterArr[4];
int intTerm = Int32.Parse(strTerm);
string strFileDataReceive = ParameterArr[5]; string strLine = strWebService + " " + strDir;
for (int i = 6; i < ParameterArr.Length; i++)
{
strLine = strLine + ParameterArr[i];
} // sleep
Thread.Sleep(intTerm * 1000); // loop
while (true)
{
// 服务OnStop时,停止线程
Boolean bSuspendEventChanged = SuspendChangedEvent.WaitOne(0, true);
if (bSuspendEventChanged)
{
break;
} CurrentTime = DateTime.Now;
TimeSpan tsCurrentTime = new TimeSpan(CurrentTime.Ticks);
TimeSpan tsLastTime = new TimeSpan(LastTime.Ticks);
TimeSpan tsMinus = tsCurrentTime.Subtract(tsLastTime).Duration(); string strSecondsMinus = tsMinus.Seconds.ToString();
int intSecondsMinus = Int32.Parse(strSecondsMinus); if (intSecondsMinus < intTerm)
{
Thread.Sleep(1);
continue;
} try
{
Process proc = new Process();
proc.StartInfo.FileName = strFileDataReceive;
proc.StartInfo.Arguments = strLine;
proc.EnableRaisingEvents = true;
proc.Start();
if (proc == null)
{
Console.WriteLine(Message.PROC_RUNING_ERR);
EventLog.WriteEntry(Message.APP_NAME, Message.PROC_RUNING_ERR,
EventLogEntryType.Information, ConstString.EVENTID, ConstString.CATEGORY); return;
}
else
{
proc.WaitForExit(); proc.Dispose();
if (proc != null)
{
proc.Close();
}
proc = null;
}
}
catch (System.Exception e)
{
// err log
Console.WriteLine(e.ToString());
EventLog.WriteEntry(Message.APP_NAME, e.ToString(),
EventLogEntryType.Information, ConstString.EVENTID, ConstString.CATEGORY);
return;
} LastTime = DateTime.Now;
}
}
#endregion ファイル監視スレッド
System.Diagnostics.Process.GetProcesses()这是我预测的能解决方法,不知道实际如何.见谅.
2. 试试强制杀死进程。
我自己写了一个exe调用,这个exe什么都不做,就是打个eventlog
这样也不行,handle还是一直在涨
可是我想知道,为什么用Process来做会有问题?? 请高人来指点啊
下面是调API做法:
try
{
if (!CreateProcess(null, new StringBuilder(strLine), null, null, false, 0, null, null, ref sInfo, ref pInfo))
{
// log
return;
}
if (WAIT_FAILED == WaitForSingleObject(pInfo.hProcess, int.MaxValue))
{
// log
}
CloseHandle(pInfo.hProcess);
CloseHandle(pInfo.hThread);
}
catch (System.Exception ex)
{
// log
return;
}