fix(legacy): complete hosted module cleanup

This commit is contained in:
2026-08-29 20:34:39 +08:00
parent 176952d52c
commit 9f5f548dea
15 changed files with 704 additions and 422 deletions
@@ -2524,7 +2524,7 @@ namespace Lskj.Control
DllName = "",
Id = this.Model.ModuleCode,
Name = this.Model.FormText,
BeforePage = ERPInfo.Instance.PageControl.SelectedTabPage
BeforePage = ERPInfo.Instance.CurrentModulePage
};
module.ModuleForm = frmGridSift;
string guid = Guid.NewGuid().ToString();
@@ -2541,8 +2541,8 @@ namespace Lskj.Control
tp.Controls.Add(frmGridSift);
tp.ShowCloseButton = DefaultBoolean.True;
tp.Dock = DockStyle.Fill;
ERPInfo.Instance.PageControl.TabPages.Add(tp);
ERPInfo.Instance.PageControl.SelectedTabPage = tp;
ERPInfo.Instance.AddModulePage(tp);
ERPInfo.Instance.CurrentModulePage = tp;
ERPInfo.Instance.ModuleForms.Add(guid, module);
frmGridSift.Show();
}
@@ -171,12 +171,12 @@ namespace Lskj.Control.BrowserSetting
dllName = MainImpl.GetDefaultValue(dllName);
if (SystemInfo.Instance.SoftOpenMode)
{
foreach (XtraTabPage tablepage in ERPInfo.Instance.PageControl.TabPages)
foreach (XtraTabPage tablepage in ERPInfo.Instance.ModulePages)
{
if (tablepage.Text == menuName)
{
MessageUtil.Show("已设置不能打开多个相同模块!");
ERPInfo.Instance.PageControl.SelectedTabPage = tablepage;
ERPInfo.Instance.CurrentModulePage = tablepage;
return;
}
}
@@ -302,8 +302,8 @@ namespace Lskj.Control.BrowserSetting
DialogResult dialogResult = mcpex.ShowDialog();
}
}
ERPInfo.Instance.PageControl.TabPages.Add(tp);
ERPInfo.Instance.PageControl.SelectedTabPage = tp;
ERPInfo.Instance.AddModulePage(tp);
ERPInfo.Instance.CurrentModulePage = tp;
ERPInfo.Instance.ModuleForms.Add(guid, module);
}
+1 -1
View File
@@ -4126,7 +4126,7 @@ namespace Lskj.Control.Model
// 添加内容
if (i == 0)
{
string guid = ERPInfo.Instance.PageControl.SelectedTabPage.Tag + "";
string guid = ERPInfo.Instance.CurrentModulePage.Tag + "";
DllModule model = ERPInfo.Instance.ModuleForms[guid];
cell.SetCellValue(model.Name);
}
@@ -1228,7 +1228,7 @@ namespace Lskj.Control.Model
paramList[12],
paramList[13]//固定添加行数据json字符串
};
XtraTabPage xtraTab = ERPInfo.Instance.PageControl != null ? ERPInfo.Instance.PageControl.SelectedTabPage : null;
XtraTabPage xtraTab = ERPInfo.Instance.CurrentModulePage != null ? ERPInfo.Instance.CurrentModulePage : null;
DllModule module = new DllModule
{
DllName = ReplaceDllFileName(dllName),
@@ -1240,12 +1240,12 @@ namespace Lskj.Control.Model
{
if (SystemInfo.Instance.SoftOpenMode)
{
foreach (XtraTabPage tablepage in ERPInfo.Instance.PageControl.TabPages)
foreach (XtraTabPage tablepage in ERPInfo.Instance.ModulePages)
{
if (tablepage.Text == model.MenuName)
{
MessageUtil.Show("已设置不能打开多个相同模块!");
ERPInfo.Instance.PageControl.SelectedTabPage = tablepage;
ERPInfo.Instance.CurrentModulePage = tablepage;
return false;
}
}
@@ -1408,8 +1408,8 @@ namespace Lskj.Control.Model
{
tp.Disposed += new EventHandler(OnTp_Disposed);
}
ERPInfo.Instance.PageControl.TabPages.Add(tp);
ERPInfo.Instance.PageControl.SelectedTabPage = tp;
ERPInfo.Instance.AddModulePage(tp);
ERPInfo.Instance.CurrentModulePage = tp;
if (isPrivateDll)
{
StaticControl.DogVerifyModuleForms.Add(tp);
@@ -15,11 +15,25 @@ namespace Lskj.Control.Model
public static class StaticBandedControl
{
public static void ReleaseModuleReferences(WinFormsControl moduleRoot, XtraTabPage modulePage, string moduleCode)
{
List<WinFormsControl> moduleControls = CollectModuleControls(moduleRoot, modulePage);
ReleaseModuleReferences(moduleControls, modulePage, moduleCode);
}
internal static void ReleaseModuleReferences(
IEnumerable<WinFormsControl> moduleControls,
XtraTabPage modulePage,
string moduleCode)
{
try
{
List<BandedGridView> bandedViews = CollectBandedGridViews(moduleRoot);
List<GridView> targetViews = CollectTargetGridViews(moduleRoot);
List<WinFormsControl> controls = new List<WinFormsControl>();
if (moduleControls != null)
foreach (WinFormsControl control in moduleControls)
if (control != null && !ContainsControl(controls, control))
controls.Add(control);
List<BandedGridView> bandedViews = CollectBandedGridViews(controls);
List<GridView> targetViews = CollectTargetGridViews(controls);
foreach (BandedGridView view in bandedViews)
BandedGridDragGrid.DisposeRegistrations(
BandedGridViewDragGridDic,
@@ -32,14 +46,10 @@ namespace Lskj.Control.Model
BandedTargetViewDragGridDic,
null,
view);
if (SourceDragBandedGridView != null &&
(bandedViews.Contains(SourceDragBandedGridView) ||
(SourceDragBandedGridView.GridControl != null && SourceDragBandedGridView.GridControl.IsDisposed)))
if (ContainsBandedGridView(bandedViews, SourceDragBandedGridView))
SourceDragBandedGridView = null;
RemoveConditionPanels(moduleCode);
while (modulePage != null && DogVerifyModuleForms.Remove(modulePage))
{
}
RemoveConditionPanels(moduleCode, controls);
RemoveProtectedReferences(controls, modulePage);
}
catch (Exception exception)
{
@@ -47,36 +57,116 @@ namespace Lskj.Control.Model
}
}
private static List<BandedGridView> CollectBandedGridViews(WinFormsControl root)
private static List<WinFormsControl> CollectModuleControls(
WinFormsControl moduleRoot,
XtraTabPage modulePage)
{
List<WinFormsControl> result = new List<WinFormsControl>();
AddModuleControls(result, moduleRoot);
if (!ReferenceEquals(moduleRoot, modulePage))
AddModuleControls(result, modulePage);
return result;
}
private static void AddModuleControls(
List<WinFormsControl> target,
WinFormsControl root)
{
if (root == null)
return;
foreach (WinFormsControl control in EnumerateControls(root))
if (!ContainsControl(target, control))
target.Add(control);
}
private static List<BandedGridView> CollectBandedGridViews(
IEnumerable<WinFormsControl> controls)
{
List<BandedGridView> result = new List<BandedGridView>();
if (root == null)
if (controls == null)
return result;
foreach (WinFormsControl control in EnumerateControls(root))
foreach (WinFormsControl control in controls)
{
DevExpress.XtraGrid.GridControl grid = control as DevExpress.XtraGrid.GridControl;
BandedGridView view = grid == null ? null : grid.MainView as BandedGridView;
if (view != null && !result.Contains(view))
result.Add(view);
if (grid == null)
continue;
try
{
AddBandedGridView(result, grid.MainView as BandedGridView);
foreach (DevExpress.XtraGrid.Views.Base.BaseView baseView in grid.ViewCollection)
AddBandedGridView(result, baseView as BandedGridView);
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine("读取旧 Banded 表格视图失败:" + exception);
}
}
return result;
}
private static List<GridView> CollectTargetGridViews(WinFormsControl root)
private static void AddBandedGridView(
List<BandedGridView> target,
BandedGridView view)
{
if (view != null && !ContainsBandedGridView(target, view))
target.Add(view);
}
private static bool ContainsBandedGridView(
IEnumerable<BandedGridView> gridViews,
BandedGridView value)
{
if (gridViews == null || value == null)
return false;
foreach (BandedGridView gridView in gridViews)
if (ReferenceEquals(gridView, value))
return true;
return false;
}
private static List<GridView> CollectTargetGridViews(
IEnumerable<WinFormsControl> controls)
{
List<GridView> result = new List<GridView>();
if (root == null)
if (controls == null)
return result;
foreach (WinFormsControl control in EnumerateControls(root))
foreach (WinFormsControl control in controls)
{
DevExpress.XtraGrid.GridControl grid = control as DevExpress.XtraGrid.GridControl;
GridView view = grid == null ? null : grid.MainView as GridView;
if (view != null && !result.Contains(view))
result.Add(view);
if (grid == null)
continue;
try
{
AddGridView(result, grid.MainView as GridView);
foreach (DevExpress.XtraGrid.Views.Base.BaseView baseView in grid.ViewCollection)
AddGridView(result, baseView as GridView);
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine("读取旧 Banded 目标表格视图失败:" + exception);
}
}
return result;
}
private static void AddGridView(List<GridView> target, GridView view)
{
if (view != null && !ContainsGridView(target, view))
target.Add(view);
}
private static bool ContainsGridView(
IEnumerable<GridView> gridViews,
GridView value)
{
if (gridViews == null || value == null)
return false;
foreach (GridView gridView in gridViews)
if (ReferenceEquals(gridView, value))
return true;
return false;
}
private static IEnumerable<WinFormsControl> EnumerateControls(WinFormsControl root)
{
yield return root;
@@ -85,16 +175,67 @@ namespace Lskj.Control.Model
yield return nested;
}
private static void RemoveConditionPanels(string moduleCode)
private static void RemoveConditionPanels(
string moduleCode,
IEnumerable<WinFormsControl> moduleControls)
{
foreach (string key in ConditionsPanelDic.Keys.ToList())
{
ModuleConditionsPanelEx panel;
if (string.Equals(key, moduleCode, StringComparison.OrdinalIgnoreCase) ||
!ConditionsPanelDic.TryGetValue(key, out panel) || panel == null || panel.IsDisposed)
if (!ConditionsPanelDic.TryGetValue(key, out panel) || panel == null || panel.IsDisposed)
{
ConditionsPanelDic.Remove(key);
continue;
}
if (string.Equals(key, moduleCode, StringComparison.OrdinalIgnoreCase) &&
IsConditionPanelOwnedByModule(panel, moduleControls))
ConditionsPanelDic.Remove(key);
}
}
private static bool IsConditionPanelOwnedByModule(
ModuleConditionsPanelEx panel,
IEnumerable<WinFormsControl> moduleControls)
{
if (panel == null)
return false;
if (ContainsControl(moduleControls, panel) || ContainsControl(moduleControls, panel.Owner))
return true;
MyControl parentControl = panel.ParentControlObj;
return parentControl != null &&
parentControl.MainGridEx != null &&
ContainsControl(moduleControls, parentControl.MainGridEx);
}
private static void RemoveProtectedReferences(
IEnumerable<WinFormsControl> moduleControls,
XtraTabPage modulePage)
{
while (modulePage != null && DogVerifyModuleForms.Remove(modulePage))
{
}
foreach (XtraTabPage item in DogVerifyModuleForms.ToList())
if (item == null || item.IsDisposed || ContainsControl(moduleControls, item))
DogVerifyModuleForms.Remove(item);
foreach (IForm item in DogVerifyNoPageForms.ToList())
if (item == null || item.SubForm == null || item.SubForm.IsDisposed ||
ContainsControl(moduleControls, item.SubForm))
DogVerifyNoPageForms.Remove(item);
}
private static bool ContainsControl(
IEnumerable<WinFormsControl> moduleControls,
WinFormsControl value)
{
if (moduleControls == null || value == null)
return false;
foreach (WinFormsControl control in moduleControls)
if (ReferenceEquals(control, value))
return true;
return false;
}
/// <summary>
/// 记录当前U盾权限窗口
/// </summary>
+121 -51
View File
@@ -435,6 +435,9 @@ namespace Lskj.Control
/// </summary>
public MyControl SearchObj { get; private set; }
private bool mModuleResourcesReleased;
// GridControlObj 可能由其他控件传入(例如左右明细表)。只有本控件
// 自己创建的表格才属于本控件,外部表格的生命周期仍由其父容器负责。
private bool _ownsGridControl = true;
/// <summary>
/// 左查询条件
/// </summary>
@@ -446,7 +449,52 @@ namespace Lskj.Control
public GridControlEx GridControlObj
{
get { return this.gcMain; }
set { this.gcMain = value; }
set { AssignGridControl(value, false); }
}
private void AssignGridControl(GridControlEx value, bool ownsGridControl)
{
GridControlEx previous = this.gcMain;
bool previousOwned = _ownsGridControl;
if (ReferenceEquals(previous, value))
{
_ownsGridControl = ownsGridControl;
return;
}
this.gcMain = value;
_ownsGridControl = ownsGridControl;
// 旧表格可能已经因为 Controls.Clear() 脱离父容器,此时 Parent 为空,
// 但它仍然属于本模块,不能仅靠解除父引用等待回收。只释放本控件
// 自己创建的实例,外部传入的表格仍由其真实拥有者负责生命周期。
if (previous != null && previousOwned)
{
try
{
if (previous.Parent != null)
{
previous.Parent.Controls.Remove(previous);
}
}
catch
{
// 父容器可能已进入 Dispose,继续释放旧表格本身。
}
try
{
if (!previous.IsDisposed)
{
previous.Dispose();
}
}
catch
{
// 控件处于部分销毁状态时不能阻止新表格完成替换。
}
}
}
/// <summary>
/// 底部多标签控件
@@ -1018,7 +1066,9 @@ namespace Lskj.Control
{
// 创建 多表头,树表格
this.panelControl1.Controls.Clear();
this.gcMain = new TreeBandedGridControlEx() { Expansion = SysModel.TreeTableExpand };
AssignGridControl(
new TreeBandedGridControlEx() { Expansion = SysModel.TreeTableExpand },
true);
this.gcMain.Model = this.Model;
this.gcMain.SysModel = this.SysModel;
(this.gcMain as TreeBandedGridControlEx).moduleModel = this.SysModel;
@@ -1045,7 +1095,7 @@ namespace Lskj.Control
if (this.gcMain.gridControl.Tag is PanelControl) panel = this.gcMain.gridControl.Tag as PanelControl;
this.panelControl1.Controls.Clear();
this.gcMain = new BandedGridControlEx();
AssignGridControl(new BandedGridControlEx(), true);
(this.gcMain as BandedGridControlEx).moduleModel = this.SysModel;
this.gcMain.Model = this.Model;
this.gcMain.SysModel = this.SysModel;
@@ -1062,7 +1112,9 @@ namespace Lskj.Control
{
// 创建树表格
this.panelControl1.Controls.Clear();
this.gcMain = new TreeGridControlEx() { Expansion = SysModel.TreeTableExpand };
AssignGridControl(
new TreeGridControlEx() { Expansion = SysModel.TreeTableExpand },
true);
this.gcMain.Model = this.Model;
this.gcMain.SysModel = this.SysModel;
this.gcMain.Dock = DockStyle.Fill;
@@ -3351,8 +3403,11 @@ namespace Lskj.Control
string[] menuArgs = new string[] { "", "", url };
string[] args = defaultArgs.Concat(menuArgs).ToArray();
IForm form = FormHelper.LoadDllForm(ResourceDynamic.PubBrower, args);
form.SubForm.WindowState = FormWindowState.Maximized;
DialogResult result = form.SubForm.ShowDialog();
using (Form subForm = form.SubForm)
{
subForm.WindowState = FormWindowState.Maximized;
subForm.ShowDialog();
}
}
else
{
@@ -3361,14 +3416,17 @@ namespace Lskj.Control
string[] menuArgs = { "", canUpdate ? "0" : "1", selectRow[_parmaryKey] + "" };
string[] args = defaultArgs.Concat(menuArgs).ToArray();
IForm form = FormHelper.LoadDllForm(ResourceDynamic.PubAdd, args);
DialogResult result = form.SubForm.ShowDialog();
//RefreshColumn();
if (result == DialogResult.OK)
using (Form subForm = form.SubForm)
{
this.SearchObj.SearchLastGrid();
if (OnSaveGridCallBack != null)
DialogResult result = subForm.ShowDialog();
//RefreshColumn();
if (result == DialogResult.OK)
{
this.OnSaveGridCallBack(null, null);
this.SearchObj.SearchLastGrid();
if (OnSaveGridCallBack != null)
{
this.OnSaveGridCallBack(null, null);
}
}
}
@@ -3785,63 +3843,75 @@ namespace Lskj.Control
searchObj.Dispose();
}
GridControlEx grid = gcMain;
try
{
if (gcMain != null)
if (grid != null)
{
if (ReferenceEquals(StaticControl.RightMenuGridView, gcMain.GridView))
if (ReferenceEquals(StaticControl.RightMenuGridView, grid.GridView))
{
StaticControl.RightMenuGridView = null;
}
if (gcMain.GridView != null)
if (grid.GridView != null)
{
gcMain.GridView.FocusedRowObjectChanged -= OnGridViewFocusedRowObjectChanged;
gcMain.GridView.ShowingEditor -= OnGridViewShowingEditor;
gcMain.GridView.DoubleClick -= OnGridViewDoubleClick;
gcMain.GridView.MouseDown -= OnGridViewMouseDown;
gcMain.GridView.MouseDown -= OnGridMouseDown;
gcMain.GridView.MouseMove -= OnGridMouseMove;
gcMain.GridView.MouseUp -= OnGridMouseUp;
grid.GridView.FocusedRowObjectChanged -= OnGridViewFocusedRowObjectChanged;
grid.GridView.ShowingEditor -= OnGridViewShowingEditor;
grid.GridView.DoubleClick -= OnGridViewDoubleClick;
grid.GridView.MouseDown -= OnGridViewMouseDown;
grid.GridView.MouseDown -= OnGridMouseDown;
grid.GridView.MouseMove -= OnGridMouseMove;
grid.GridView.MouseUp -= OnGridMouseUp;
}
if (gcMain.CustomGroupBandEx != null)
if (grid.CustomGroupBandEx != null)
{
gcMain.CustomGroupBandEx.GridView.FocusedRowObjectChanged -= OnGridViewFocusedRowObjectChanged;
gcMain.CustomGroupBandEx.GridView.ShowingEditor -= OnGridViewShowingEditor;
grid.CustomGroupBandEx.GridView.FocusedRowObjectChanged -= OnGridViewFocusedRowObjectChanged;
grid.CustomGroupBandEx.GridView.ShowingEditor -= OnGridViewShowingEditor;
}
if (gcMain.ParentControl != null &&
ReferenceEquals(gcMain.ParentControl, searchObj))
if (grid.ParentControl != null &&
ReferenceEquals(grid.ParentControl, searchObj))
{
gcMain.ParentControl = null;
grid.ParentControl = null;
}
gcMain.Model = null;
gcMain.SysModel = null;
try
{
gcMain.ReleaseResourcesForDispose();
}
catch
{
// Resource cleanup must not block the module close path.
}
try
{
if (!gcMain.IsDisposed)
{
gcMain.Dispose();
}
}
catch
{
// A partially disposed grid must not block the module close path.
}
gcMain = null;
}
}
catch
{
// A partially disposed DevExpress view must not stop module cleanup.
}
finally
{
// 自有表格的生命周期由本模块负责。先从父容器移除,再显式释放,
// 防止调用方此前执行 Controls.Clear() 后留下孤立 GridControlEx。
bool ownsGrid = _ownsGridControl;
gcMain = null;
_ownsGridControl = false;
if (ownsGrid && grid != null)
{
try
{
if (grid.Parent != null)
{
grid.Parent.Controls.Remove(grid);
}
}
catch
{
// 父容器可能已进入 Dispose,继续释放表格本身。
}
try
{
if (!grid.IsDisposed)
{
grid.Dispose();
}
}
catch
{
// 部分销毁的 DevExpress 控件不能阻止模块其余资源清理。
}
}
}
try
{
+5 -4
View File
@@ -34,14 +34,15 @@ namespace Lskj.Control
/// </summary>
private void InitializeComponent()
{
this.pm_print = new DevExpress.XtraBars.PopupMenu();
this.barManager1 = new DevExpress.XtraBars.BarManager();
this.components = new System.ComponentModel.Container();
this.pm_print = new DevExpress.XtraBars.PopupMenu(this.components);
this.barManager1 = new DevExpress.XtraBars.BarManager(this.components);
this.barDockControlTop = new DevExpress.XtraBars.BarDockControl();
this.barDockControlBottom = new DevExpress.XtraBars.BarDockControl();
this.barDockControlLeft = new DevExpress.XtraBars.BarDockControl();
this.barDockControlRight = new DevExpress.XtraBars.BarDockControl();
this.pm_common = new DevExpress.XtraBars.PopupMenu();
this.pm_oper = new DevExpress.XtraBars.PopupMenu();
this.pm_common = new DevExpress.XtraBars.PopupMenu(this.components);
this.pm_oper = new DevExpress.XtraBars.PopupMenu(this.components);
this.panel1 = new System.Windows.Forms.Panel();
this.scc_container = new DevExpress.XtraEditors.SplitContainerControl();
this.panel_Main = new System.Windows.Forms.Panel();