私のプログラムでは、データベースに接続できない場合にデータをファイルに書き込み、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);
}