JD2022-TU1/main/tools/framework/JD.Controls/RefreshTask.cs

60 lines
No EOL
1.6 KiB
C#

using System;
using JD.Threading;
namespace JD.Controls
{
/// <summary>
/// A helper to limit the number of calls to refresh method in the UI.
/// It postpones the refresh on the MessageQueue so the task is executed only once for a lot of changes in the bound model.
/// </summary>
public class RefreshTask : WakeUpThreadBase
{
private readonly Action _refreshAction;
private bool _disposed;
public RefreshTask(Action refreshAction)
{
_refreshAction = refreshAction;
}
/// <summary>
/// Closes the task to stop sending events and cancels the last remaining event.
/// </summary>
public void Dispose()
{
_disposed = true;
}
protected override void DoWork()
{
if (!_disposed)
{
_refreshAction();
}
}
/// <summary>
/// Executing the action on the message queue.
/// </summary>
protected override void QueueUserWorkItem(System.Threading.WaitCallback waitCallBack)
{
TeaBox.ObjectModel.Application.Main.Dispatcher.DispatchAsync(() => waitCallBack(null), forceAsync:true);
}
/// <summary>
/// Invokes the refresh
/// </summary>
public void Refresh()
{
base.WakeUp();
}
/// <summary>
/// Users should bind this method to an event.
/// </summary>
public void ModelChanged(object sender, EventArgs e)
{
Refresh();
}
}
}