fix: 收敛旧模块资源释放与诊断

This commit is contained in:
2026-08-15 10:13:08 +08:00
parent 222ae3c08a
commit c49015d390
60 changed files with 4218 additions and 409 deletions
+114 -7
View File
@@ -560,6 +560,71 @@ namespace Lskj.Control
this.gridView.MouseWheel += GridView_MouseWheel;
}
private DbCommandBuilder _adapterCommandBuilder;
protected static void DisposeDataAdapter(DbDataAdapter adapter)
{
if (adapter == null)
{
return;
}
DbCommand[] commands =
{
adapter.SelectCommand,
adapter.InsertCommand,
adapter.UpdateCommand,
adapter.DeleteCommand
};
foreach (DbCommand command in commands)
{
try
{
command?.Dispose();
}
catch
{
// Resource cleanup must not break control disposal or a new query.
}
}
try
{
adapter.Dispose();
}
catch
{
// Resource cleanup must not break control disposal or a new query.
}
}
protected void SetAdapterCommandBuilder(DbDataAdapter adapter)
{
DisposeAdapterCommandBuilder();
if (adapter == null)
{
return;
}
_adapterCommandBuilder = SqlHelper.dbFactory.CreateCommandBuilder();
_adapterCommandBuilder.DataAdapter = adapter;
}
protected void DisposeAdapterCommandBuilder()
{
DbCommandBuilder commandBuilder = _adapterCommandBuilder;
_adapterCommandBuilder = null;
try
{
commandBuilder?.Dispose();
}
catch
{
// Resource cleanup must not break control disposal or a new query.
}
}
private void DisposeGridResources()
{
if (_gridResourcesDisposed)
@@ -574,9 +639,23 @@ namespace Lskj.Control
mTimer.Dispose();
mTimer = null;
}
try
{
if (GridControl != null)
{
GridControl.DataSource = null;
}
}
catch
{
// The inner DevExpress control may already be disposed.
}
DisposeColumnImageCaches(ColumnList);
DisposeRightMenuButtonImages();
ClearBarcodeImageCache();
DisposeAdapterCommandBuilder();
DisposeDataAdapter(AdapterObj);
AdapterObj = null;
mControlSourceDic.Clear();
mControlList.Clear();
ColumnList.Clear();
@@ -585,6 +664,9 @@ namespace Lskj.Control
ImageList.Clear();
RepeatedVerificationNames.Clear();
PromptMessage.Clear();
ParentControl = null;
Model = null;
SysModel = null;
if (_mainGridToolTipController != null)
{
_mainGridToolTipController.BeforeShow -= MainGvTool_BeforeShow;
@@ -2032,7 +2114,7 @@ namespace Lskj.Control
//}));
Task<DataTable> customColumnByDatabaseTask = cachesDic.AddTask(this, "CustomColumnByDatabase", new Task<DataTable>(() =>
{
return this.GridView.GetCustomColumnByDatabase(customColumKey);
return GridExtend.GetCustomColumnByDatabase(customColumKey);
}));
}
@@ -2167,12 +2249,14 @@ namespace Lskj.Control
{
if (adapter == null) return 0;
DbDataAdapter previousAdapter = this.AdapterObj;
bool adapterAssigned = false;
DataSet dataSet = null;
try
{
this.AdapterObj = adapter;
DataSet dataSet = new DataSet();
dataSet = new DataSet();
// 重连配置
int maxRetryCount = 2; // 次数
@@ -2211,7 +2295,23 @@ namespace Lskj.Control
}
}
DataTable dt = dataSet.Tables.Count > 0 ? dataSet.Tables[0] : null;
int rowCount = dt == null ? 0 : dt.Rows.Count;
if (dt != null)
{
// Detach the result table before releasing the temporary DataSet.
dataSet.Tables.Remove(dt);
}
dataSet.Dispose();
dataSet = null;
DisposeAdapterCommandBuilder();
if (previousAdapter != null && !ReferenceEquals(previousAdapter, adapter))
{
DisposeDataAdapter(previousAdapter);
}
this.AdapterObj = adapter;
adapterAssigned = true;
if (this.ColumnList == null || this.ColumnList.Count == 0)
{
@@ -2226,12 +2326,10 @@ namespace Lskj.Control
this.gridView.Columns.Clear();
}
}
DbCommandBuilder cb = SqlHelper.dbFactory.CreateCommandBuilder();
cb.DataAdapter = this.AdapterObj;
SetAdapterCommandBuilder(this.AdapterObj);
//this.GridControl.DataSource = dataSet.Tables[0];
//解决大小写问题后在赋值
DataTable dt = dataSet.Tables[0];
if (dt != null && this.gridView.Columns.Count > 0)
{
foreach (DataColumn column in dt.Columns)
@@ -2272,12 +2370,20 @@ namespace Lskj.Control
this.SetAutoColumns();
if (selectRowHandler)
this.gridView.SelectRowHandler(this.gridView.FocusedRowHandle);
return dataSet.Tables[0] != null ? dataSet.Tables[0].Rows.Count : 0;
return rowCount;
}
catch (Exception ex)
{
if (!adapterAssigned)
{
DisposeDataAdapter(adapter);
}
MessageUtil.Show(ex);
}
finally
{
dataSet?.Dispose();
}
return 0;
}
@@ -5532,6 +5638,7 @@ namespace Lskj.Control
/// <param name="e">事件参数。</param>
private void OnGridControlExDisposed(object sender, EventArgs e)
{
DisposeGridResources();
foreach (FrmModelLookUp2 modelLookUp in selectReturnModelLookUpCache.Values.Distinct())
{
if (modelLookUp != null && !modelLookUp.IsDisposed)