From 596389540a602329116956dc7a7c8dfa53cda659 Mon Sep 17 00:00:00 2001
From: C3204 <zhengyabo@lanpucloud.cn>
Date: 星期三, 31 十二月 2025 11:19:49 +0800
Subject: [PATCH] 修复一部分内存泄露bug。
---
LB_SmartVision/VisionForm.cs | 193 +++++++++++++++++++++++++++++++++++-------------
1 files changed, 140 insertions(+), 53 deletions(-)
diff --git a/LB_SmartVision/VisionForm.cs b/LB_SmartVision/VisionForm.cs
index b014f34..37fd869 100644
--- a/LB_SmartVision/VisionForm.cs
+++ b/LB_SmartVision/VisionForm.cs
@@ -40,7 +40,7 @@
{
public partial class VisionForm : Form
{
- AllProcessesPage AllProcessesPage = new AllProcessesPage();
+ AllProcessesPage AllProcessesPages = new AllProcessesPage();
CamerasEditPage CamerasEditPage = new CamerasEditPage();
CommunicatorsEditPage CommunicatorsEditPage = new CommunicatorsEditPage();
SettingEditPage SettingEditPage = new SettingEditPage();
@@ -66,7 +66,9 @@
//鏈�寮�濮嬪氨娓呯┖鎵�鏈塗ab椤�
materialTabControl.TabPages.Clear();
- materialTabControl.Controls.Add(AllProcessesPage);
+ AllProcessesPages.controlsPanel.Dock = DockStyle.Fill;
+ materialTabControl.Controls.Add(AllProcessesPages);
+
CamerasEditPage.LogInfo += LogInfo;
materialTabControl.Controls.Add(new MyPage(CamerasEditPage));
@@ -90,7 +92,6 @@
{
//materialTabControl.TabPages[i].Font= new Font("Microsoft YaHei UI", 18F, FontStyle.Regular, GraphicsUnit.Point, 0);
}
-
materialTabSelector.BaseTabControl = materialTabControl;
//materialTabSelector.Font = new Font("Microsoft YaHei UI", 18F, FontStyle.Regular, GraphicsUnit.Point, 0);
}
@@ -241,7 +242,7 @@
{
return;
}
- string strInfo = DateTime.Now.ToString("[yyyy:MM:HH:mm:ss:fff] ");
+ string strInfo = DateTime.Now.ToString("[yyyy:MM:dd:HH:mm:ss:fff] ");
strInfo += strLog;
if (infoType != LogInfoType.NOSHOW)
{
@@ -257,19 +258,30 @@
switch (infoType)
{
case LogInfoType.INFO:
- this.rich_Info.SelectionColor = Color.Wheat;
- break;
+ {
+ this.rich_Info.SelectionColor = Color.Wheat;
+ AsyncLogHelper.Info(strLog);
+ break;
+ }
case LogInfoType.WARN:
- this.rich_Info.SelectionColor = Color.LightGoldenrodYellow;
- break;
+ {
+ this.rich_Info.SelectionColor = Color.Yellow;
+ AsyncLogHelper.Warn(strLog);
+ break;
+ }
case LogInfoType.PASS:
- this.rich_Info.SelectionColor = Color.Green;
- break;
+ {
+ this.rich_Info.SelectionColor = Color.Green;
+ AsyncLogHelper.Info(strLog);
+ break;
+ }
case LogInfoType.ERROR:
- this.rich_Info.SelectionColor = Color.Red;
- break;
+ {
+ this.rich_Info.SelectionColor = Color.Red;
+ AsyncLogHelper.Error(strLog);
+ break;
+ }
}
-
// 鏇存柊 UI 鎺т欢锛屾瘮濡傛樉绀烘帴鏀跺埌鐨勬秷鎭�
this.rich_Info.AppendText(strInfo);
this.rich_Info.AppendText("\r\n");
@@ -281,32 +293,43 @@
{
if (this.rich_Info.Lines.Length > 1000)
+ {
this.rich_Info.Clear();
-
+ }
// 濡傛灉宸茬粡鍦� UI 绾跨▼涓婏紝鐩存帴鏇存柊 UI
switch (infoType)
{
case LogInfoType.INFO:
- this.rich_Info.SelectionColor = Color.Wheat;
- break;
+ {
+ this.rich_Info.SelectionColor = Color.Wheat;
+ AsyncLogHelper.Info(strLog);
+ break;
+ }
case LogInfoType.WARN:
- this.rich_Info.SelectionColor = Color.Yellow;
- break;
+ {
+ this.rich_Info.SelectionColor = Color.Yellow;
+ AsyncLogHelper.Warn(strLog);
+ break;
+ }
case LogInfoType.PASS:
- this.rich_Info.SelectionColor = Color.Green;
- break;
+ {
+ this.rich_Info.SelectionColor = Color.Green;
+ AsyncLogHelper.Info(strLog);
+ break;
+ }
case LogInfoType.ERROR:
- this.rich_Info.SelectionColor = Color.Red;
- break;
+ {
+ this.rich_Info.SelectionColor = Color.Red;
+ AsyncLogHelper.Error(strLog);
+ break;
+ }
}
-
this.rich_Info.AppendText(strInfo);
this.rich_Info.AppendText("\r\n");
this.rich_Info.SelectionStart = this.rich_Info.Text.Length;
this.rich_Info.ScrollToCaret();
}
}
- AsyncLogHelper.Info(strLog);
}
public static bool SaveAllLayout()
@@ -335,9 +358,7 @@
}
catch { }
}
-
File.WriteAllText(GlobalVar.allLayoutPath, strJson, Encoding.UTF8);
-
return true;
}
catch { return false; }
@@ -501,9 +522,9 @@
{
XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
string[] paths = {
- @"鐢熶骇鏃ュ織\Run_Log",
- @"鐢熶骇鏃ュ織\Debug_Log",
- @"鐢熶骇鏃ュ織\Error_Log",
+ @"鐢熶骇鏃ュ織\Run",
+ @"鐢熶骇鏃ュ織\Debug",
+ @"鐢熶骇鏃ュ織\Error",
@"鐢熶骇鏃ュ織\Fatal",
@"鐢熶骇鏃ュ織\Warn",
};
@@ -519,72 +540,99 @@
}
LB_SmartVision.Tool.Tool.ReadStringConfig("User ID", out string User_ID);
LB_SmartVision.Tool.Tool.ReadStringConfig("Password", out string Password);
-
GlobalVar.strProductName = DateBaseName;
-
//鍔犺浇閫氳
foreach (BaseCommunicator com in GlobalVar.dicCommunicators.Values)
+ {
com.Disconnect();
+ }
GlobalVar.dicCommunicators.Clear();
if (LoadAllCommunicators(GlobalVar.allCommunicatorsConnectionStringPath))
+ {
LogInfo("閫氳鍔犺浇鎴愬姛", LogInfoType.PASS);
+ }
else
+ {
LogInfo("閫氳鍔犺浇澶辫触", LogInfoType.ERROR);
-
+ }
//鍔犺浇鐩告満
foreach (BaseCamera camera in GlobalVar.dicCameras.Values)
+ {
camera.Dispose();
+ }
GlobalVar.dicCameras.Clear();
if (LoadAllCameras(GlobalVar.allCamerasConnectionStringPath))
+ {
LogInfo("鐩告満鍔犺浇鎴愬姛", LogInfoType.PASS);
+ }
else
+ {
LogInfo("鐩告満鍔犺浇澶辫触", LogInfoType.ERROR);
-
- //蹇呴』鍏堝姞杞界浉鏈哄拰閫氳绔彛,鍥犱负娴佺▼鍔犺浇杩囩▼涓細鐢ㄥ埌鐩告満鍜岄�氳鍙�
-
+ }
//鍔犺浇鍏ㄥ眬鍙橀噺
IProcess.dicGlobalVars.Clear();
if (LoadAllProcessVars(GlobalVar.allProcessVarsPath))
+ {
LogInfo("鍏ㄥ眬鍙橀噺鍔犺浇鎴愬姛", LogInfoType.PASS);
+ }
else
+ {
LogInfo("鍏ㄥ眬鍙橀噺鍔犺浇澶辫触", LogInfoType.ERROR);
+ }
//鍔犺浇娴佺▼
GlobalVar.dicProcesses.Clear();
if (LoadAllProcess(GlobalVar.allProcessConnectionStringPath))
+ {
LogInfo("娴佺▼鍔犺浇鎴愬姛", LogInfoType.PASS);
+ }
else
+ {
LogInfo("娴佺▼鍔犺浇澶辫触", LogInfoType.ERROR);
-
+ }
//鍔犺浇瑙﹀彂璁剧疆
if (LoadAllProcessSetting(GlobalVar.allProcessSettingStringPath))
+ {
LogInfo("娴佺▼璁剧疆鍔犺浇鎴愬姛", LogInfoType.PASS);
+ }
else
+ {
LogInfo("娴佺▼璁剧疆鍔犺浇澶辫触", LogInfoType.ERROR);
+ }
//鍔犺浇娴佺▼甯冨眬
GlobalVar.dicLayout.Clear();
if (LoadAllLayout(GlobalVar.allLayoutPath))
+ {
LogInfo("娴佺▼甯冨眬鍔犺浇鎴愬姛", LogInfoType.PASS);
+ }
else
+ {
LogInfo("娴佺▼甯冨眬鍔犺浇澶辫触", LogInfoType.ERROR);
+ }
//鍔犺浇娴佺▼琛ㄦ牸
GlobalVar.dicCsvSetting.Clear();
if (LoadAllCsv(GlobalVar.allCsvPath))
+ {
LogInfo("娴佺▼琛ㄦ牸鍔犺浇鎴愬姛", LogInfoType.PASS);
+ }
else
+ {
LogInfo("娴佺▼琛ㄦ牸鍔犺浇澶辫触", LogInfoType.ERROR);
+ }
//鏄剧ず鎵�鏈変骇鍝�
com_ProductName.Items.Clear();
LB_SmartVision.Tool.Tool.ReadStringConfig("浜у搧鍒楄〃", out string Products);
List<string> lstProduct = (Products.Split(',')).ToList();
foreach (string DatabaseName in lstProduct)
+ {
com_ProductName.Items.Add(DatabaseName);
+ }
com_ProductName.Items.Add("鏂板");
-
com_ProductName.Text = GlobalVar.strProductName;
+ this.WindowState = FormWindowState.Maximized;
}
public void SaveAllSetting()
@@ -614,7 +662,6 @@
strJson = streamReader.ReadToEnd();
streamReader.Close();
}
-
GlobalVar.allCamerasConnectionString = JsonConvert.DeserializeObject<ConcurrentDictionary<string, string>>(strJson);
if (GlobalVar.allCamerasConnectionString == null)
{
@@ -628,16 +675,21 @@
switch (brand)
{
case CameraBrand.HRCamera:
- camera = new HRCamera();
- break;
+ {
+ camera = new HRCamera();
+ break;
+ }
case CameraBrand.LBCamera:
- //camera = new LBCamera();
- break;
+ {
+ //camera = new LBCamera();
+ break;
+ }
default:
- MessageBox.Show($"[{CameraConnectionString.Key}]鍝佺墝涓嶆敮鎸�!", "寮傚父");
- continue;
+ {
+ MessageBox.Show($"[{CameraConnectionString.Key}]鍝佺墝涓嶆敮鎸�!", "寮傚父");
+ continue;
+ }
}
-
camera.SN = CameraConnectionString.Key;
if (!camera.InitDevice(CameraConnectionString.Key, this.Handle))
{
@@ -665,8 +717,9 @@
string CameraBrand = item.Value.Brand.ToString();//"1111"
if (string.IsNullOrEmpty(CameraSN) || string.IsNullOrEmpty(CameraBrand))
+ {
break;
-
+ }
GlobalVar.allCamerasConnectionString.TryAdd(CameraSN, CameraBrand);
}
var settings = new JsonSerializerSettings
@@ -776,9 +829,13 @@
Communicator.CommunicatorConnections.Add("绔彛", PORT);
Communicator.CommunicatorName = CommunicatorName;
if (!Communicator.Connect())
+ {
LogInfo($"鍒濆鍖栭�氳鍙{CommunicatorName}]澶辫触,鍘熷洜鏄瘂Communicator.Msg}", LogInfoType.ERROR);
+ }
else
+ {
LogInfo($"鍒濆鍖栭�氳鍙{CommunicatorName}]鎴愬姛", LogInfoType.PASS);
+ }
GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator);
}
else
@@ -810,7 +867,9 @@
string PORT = match.Groups[3].Value; // "1111"
if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
+ {
break;
+ }
//鍒╃敤鍙嶅皠鍒涘缓瀹炰緥
Type type = IProcess.GetExecutingAssembly().GetType(ClassName);
@@ -831,10 +890,13 @@
Communicator.CommunicatorConnections.Add("绔彛", PORT);
Communicator.CommunicatorName = CommunicatorName;
if (!Communicator.Connect())
+ {
LogInfo($"鍒濆鍖栭�氳鍙{CommunicatorName}]澶辫触,鍘熷洜鏄瘂Communicator.Msg}", LogInfoType.ERROR);
+ }
else
+ {
LogInfo($"鍒濆鍖栭�氳鍙{CommunicatorName}]鎴愬姛", LogInfoType.PASS);
-
+ }
GlobalVar.dicCommunicators.TryAdd(CommunicatorName, Communicator);
}
else
@@ -863,12 +925,12 @@
string PORT = item.Value.CommunicatorConnections["绔彛"].ToString();//"1111"
if (string.IsNullOrEmpty(ClassName) || string.IsNullOrEmpty(IP) || string.IsNullOrEmpty(PORT))
+ {
break;
-
+ }
string CommunicatorConnectionString = $"({ClassName})[{IP}][{PORT}]";
GlobalVar.allCommunicatorsConnectionString.TryAdd(item.Key, CommunicatorConnectionString);
}
-
var settings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
@@ -919,17 +981,19 @@
List<string> lstProcessName = JsonConvert.DeserializeObject<List<string>>(strJson);
if (lstProcessName == null)
+ {
return false;
-
+ }
// 浣跨敤鏂瑰紡
var sortedKeys = lstProcessName
.OrderBy(k => k, new NaturalStringComparer())
.ToList();
GlobalVar.dicProcesses.Clear();
foreach (var ProcessName in sortedKeys)
+ {
GlobalVar.dicProcesses.TryAdd(ProcessName
, new ProcessRunBll(ProcessName, GlobalVar.dicCameras, GlobalVar.dicCommunicators));
-
+ }
return true;
}
catch { return false; }
@@ -966,7 +1030,9 @@
foreach (var process in GlobalVar.dicProcesses.Values)
{
if (!process.Save(out string msg))
+ {
LogInfo($"娴佺▼[{process.Name}]淇濆瓨澶辫触,鍘熷洜:{msg}", LogInfoType.NOSHOW);
+ }
}
try
@@ -1380,12 +1446,12 @@
{
string title = layout.Title;
string strImagePath = layout.SaveImageDir;
- if (!AllProcessesPage.dicProcessControls.ContainsKey(title))
+ if (!AllProcessesPages.dicProcessControls.ContainsKey(title))
{
continue;
}
RunBll.GetImage(layout, out InputImage, out RecordImage);
- AllProcessesPage.dicProcessControls[title].ShowHoImage(RecordImage);
+ AllProcessesPages.dicProcessControls[title].ShowHoImage(RecordImage);
if (!string.IsNullOrEmpty(layout.SaveImageDir))
{
string fileNameHead = layout.SaveImageHead;
@@ -1440,6 +1506,27 @@
}
});
}
+
+ private void VisionForm_FormClosing(object sender, FormClosingEventArgs e)
+ {
+ SaveAllSetting();
+ if (MessageBox.Show("鏄惁鍏抽棴杞欢锛�", "鎻愮ず", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)//
+ {
+ e.Cancel = true;
+ return;
+ }
+ //鍏抽棴绐椾綋閲婃斁璧勬簮
+ AsyncLogHelper.Dispose();
+ foreach (BaseCamera camera in GlobalVar.dicCameras.Values)
+ {
+ camera.Dispose();
+ }
+ foreach (BaseCommunicator communicator in GlobalVar.dicCommunicators.Values)
+ {
+ communicator.Disconnect();
+ }
+ FormClosing -= VisionForm_FormClosing;
+ }
}
}
--
Gitblit v1.9.3