fix: 收敛旧控件资源与后台线程生命周期

This commit is contained in:
2026-08-11 14:12:18 +08:00
parent f1ce6ff658
commit a9c986f9d5
20 changed files with 1145 additions and 380 deletions
@@ -46,6 +46,13 @@ namespace Lskj.Control
/// </summary>
private bool _working = false;
private List<string> _queryQueue = new List<string>();
private LatestValueWorker<PopupQueryRequest> _queryWorker;
private sealed class PopupQueryRequest
{
public string SqlValue;
public bool IsNewPopup;
}
/// <summary>
/// The _popup
/// </summary>
@@ -307,7 +314,9 @@ namespace Lskj.Control
{
InitializeComponent();
InitializePopup();
_queryWorker = new LatestValueWorker<PopupQueryRequest>(ExecuteLatestPopupQuery);
InitializeEvent();
this.Disposed += OnDisposed;
//this.Properties.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Down;
}
@@ -361,9 +370,7 @@ namespace Lskj.Control
this.ShowPopup();
}
Thread queryThread = new Thread(CalcPopupSize);
queryThread.IsBackground = true;
queryThread.Start(Popup);
ThreadPool.QueueUserWorkItem(CalcPopupSize, Popup);
}
/// <summary>
/// <para>说明:计算弹出控件位置</para>
@@ -551,16 +558,7 @@ namespace Lskj.Control
sqlValue = ControlObj.ReplaceControlValue(sourceSql);
}
if (!this._working)
{
Thread thread = new Thread(new ParameterizedThreadStart(RefreshPopupDataSource));
thread.IsBackground = true;
thread.Start(sqlValue);
}
else
{
this._queryQueue.Add(sqlValue);
}
QueuePopupQuery(sqlValue, false);
}
}
@@ -656,9 +654,9 @@ namespace Lskj.Control
string sql = MainImpl.GetDefaultValue(this._queryQueue.Last());
sql = ReplaceHelper.ReplaceParam(sql);
// 在检索过程中还存在输入数据,则拿最后一次数据在进行匹配一次
Thread thread = new Thread(new ParameterizedThreadStart(RefreshPopupDataSource));
thread.IsBackground = true;
thread.Start(sql);
ThreadPool.QueueUserWorkItem(
RefreshPopupDataSource,
sql);
this._queryQueue.Clear();
}
@@ -1403,17 +1401,78 @@ namespace Lskj.Control
sqlValue = ControlObj.ReplaceControlValue(sourceSql.Replace("#", ""));
}
if (!this._working)
QueuePopupQuery(sqlValue, true);
}
}
private void QueuePopupQuery(string sqlValue, bool isNewPopup)
{
if (_queryWorker == null || IsDisposed || Disposing)
{
return;
}
_working = true;
if (Popup != null)
{
Popup.LabelObj.Text = "正在筛选数据,请稍后...";
Popup.BottomPanelObj.Visible = true;
Popup.GridControlObj.DataSourceTable().Clear();
}
_queryWorker.Queue(new PopupQueryRequest
{
SqlValue = sqlValue,
IsNewPopup = isNewPopup
});
}
private void ExecuteLatestPopupQuery(PopupQueryRequest request, int version)
{
DateTime beginTime = DateTime.Now;
DataTable table = new DataTable();
if (request != null && !string.IsNullOrWhiteSpace(request.SqlValue))
{
try
{
Thread thread = new Thread(new ParameterizedThreadStart(RefreshPopupDataSourceNew));
thread.IsBackground = true;
thread.Start(sqlValue);
table = SqlHelper.ExecuteDataTable(request.SqlValue);
}
else
catch
{
this._queryQueue.Add(sqlValue);
table = new DataTable();
}
}
DateTime endTime = DateTime.Now;
if (_queryWorker == null || !_queryWorker.IsCurrent(version) || IsDisposed || Disposing || !IsHandleCreated)
{
return;
}
try
{
BeginInvoke(new MethodInvoker(delegate
{
if (_queryWorker == null || !_queryWorker.IsCurrent(version) || IsDisposed || Disposing || Popup == null)
{
return;
}
int second = Convert.ToInt32((endTime - beginTime).TotalSeconds);
string tipMsg = "数据筛选完成.";
Popup.LabelObj.Text = second > 0 ? string.Format(tipMsg + "(耗时{0}秒)", second) : tipMsg;
Popup.GridControlObj.DataSource = table;
_working = false;
if (request != null && !request.IsNewPopup)
{
CalcFormAndGrid();
}
}));
}
catch (InvalidOperationException)
{
}
}
private void RefreshPopupDataSourceNew(object sqlValue)
@@ -1449,9 +1508,9 @@ namespace Lskj.Control
if (this._queryQueue.Count > 0)
{
// 在检索过程中还存在输入数据,则拿最后一次数据在进行匹配一次
Thread thread = new Thread(new ParameterizedThreadStart(RefreshPopupDataSourceNew));
thread.IsBackground = true;
thread.Start(this._queryQueue.Last());
ThreadPool.QueueUserWorkItem(
RefreshPopupDataSourceNew,
this._queryQueue.Last());
this._queryQueue.Clear();
}
@@ -1478,5 +1537,21 @@ namespace Lskj.Control
}
}
private void OnDisposed(object sender, EventArgs e)
{
if (_queryWorker != null)
{
_queryWorker.Dispose();
_queryWorker = null;
}
_working = false;
_queryQueue.Clear();
if (_popup != null)
{
_popup.Dispose();
_popup = null;
}
}
}
}
}