3
在我的程序中,如果我无法连接到数据库,我将数据写入文件,而且我有一个单独的线程,它具有计时器,该计时器在每25秒后检查连接可用性,并且如果它可以连接,则将数据从文件传输到主分区并删除文件。 问题是我永远不会停止这个计时器,这会导致内存泄漏吗? 如果我只是运行了一个程序,监控任务管理器,我可以看到内存使用率不断增加,如果我停止计时器,然后运行我的应用程序,则内存稳定可以持续引发内存泄漏的计时器吗?
public BackgroundWorker()
{
_backgroundWorkerThread = new Thread(new ThreadStart(ThreadEntryPoint));
_timer = new SWF.Timer();
_timer.Tick += new EventHandler(_timer_Tick);
_timer.Interval = 25 * 1000;
_timer.Enabled = true;
}
void _timer_Tick(object sender, EventArgs e)
{
bool lanAvailabe = NetworkInterface.GetIsNetworkAvailable();
if (lanAvailabe)
{
if (!GetListOfFiles())
{
return;
}
}
else
return;
}
实施GetListofFiles()
的private bool GetListOfFiles()
{
string sourceDirectory = pathOfXmlFiles;
if (!Directory.Exists(sourceDirectory))
{
return false;
}
var xmlFiles = Directory.GetFiles(sourceDirectory, "*.xml");
if (!xmlFiles.Any())
{
return false;
}
foreach (var item in xmlFiles)
{
ReadXmlFile(item);
}
foreach (var item in xmlFiles)
{
if (_writtenToDb)
{
File.Delete(item);
}
}
return true;
}
方法读取XML文件
private void ReadXmlFile(string filename)
{
string[] patientInfo = new string[15];
using (StreamReader sr = new StreamReader(filename, Encoding.Default))
{
String line;
line = sr.ReadToEnd();
if (line.IndexOf("<ID>") > 0)
{
patientInfo[0] = GetTagValue(line, "<ID>", "</ID>");
}
if (line.IndexOf("<PatientID>") > 0)
{
patientInfo[1] = GetTagValue(line, "<PatientID>", "</PatientID>");
}
if (line.IndexOf("<PatientName>") > 0)
{
patientInfo[2] = GetTagValue(line, "<PatientName>", "</PatientName>");
}
if (line.IndexOf("<Room>") > 0)
{
patientInfo[3] = GetTagValue(line, "<Room>", "</Room>");
}
}
WriteToDb(patientInfo);
}
是的,当然是一个永无止境的计时器*可能*导致内存泄漏。但为了回答你的问题,我们需要看看'GetListOfFiles()'的实现。 –
你可以告诉SWF命名空间吗? – tia
@tia使用SWF = System.Windows.Forms; –