c# - 如果我有新的 UI 更新,如何停止预定的 UI 更新?
问题描述
所以,我有一个可调整大小的窗口,它根据它计算的实时值绘制一个图表。如果我使窗口更大,它会重绘和缩放所有内容,当图形达到新的 maxY 和 maxX 时,也会使用这些作为参考点来确定其他点的位置。这工作正常,但如果我调整窗口大小,并且安排了很多更新,窗口调整大小很好,但图表模仿了它应该在几秒钟前(最多 4 秒)做的事情。
如果我理解正确的话,每次我调用被阻塞的 UI 线程并给他新的坐标来绘制时,它都会完成当前的坐标,然后继续下一个坐标。我明白你为什么想要这样,但由于图表或多或少地不断缩放,无论如何它都会在每次更新时自行删除,所以我会节省相当多的处理能力和运行时间,如果我能完成所有当前任务并跳过所有累积的任务到最新的,因为存储的任务已经过时了
有没有办法做到这一点?
我想到了 3 件可能有效的事情,其中第 3 件是唯一的一件,我知道它会起作用,或者更确切地说,是可能的,即使速度很慢:
// (1) If there is a way to check if the UI thread is busy, I could just not send
// a new request to draw another line
if(!uithread.busy)
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
new Action(() => this.DrawGraph(new List<List<Coordinates>> { CoordinateListA, CoordinateListB })));
}
// (2) As soon as UI thread finishes the current task, it jumps to the newest one
// (3) In my opinion the ugliest solution, I check how long the last drawing took and
// don't send any draw-requests in that time. I will just wait that certain time and
// just then send him the newest results to draw and measure how long that took
如果没有更好的解决方案,我想我会选择(3),但是由于我希望有另一种方法,所以我想问一下这里的其他人是否有类似的问题。
解决方案
所以我修好了。我的架构保存了所有日志,所以我不需要保存坐标,如果我需要新值,我可以在每次需要新坐标时从日志中计算它们。像这样:
// beginning of the class
private bool _isDrawing = false;
if (!_graphIsDrawing)
{
_graphIsDrawing = true;
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background,
new Action(() => this.DrawGraph(new List<List<Coordinates>> { CoordinateListA, CoordinateListB }, scalingFactors, canvasSize)));
//I need to invoke, since I am working with multiple threads here. Else it
//would be enough to just call 'this.DrawGraph(...)'
}
///////////
public void DrawGraph(List<List<Coordinates>> listOfGraphs, float[] scalingFactor, int[] canvasSize)
{
lock (_graphDrawLock)
{
this._AlgorithmRuntimeViewA.DrawGraph(listOfGraphs[0], scalingFactor, canvasSize);
this._AlgorithmRuntimeViewB.DrawGraph(listOfGraphs[1], scalingFactor, canvasSize);
_graphIsDrawing = false;
}
}
在这里我再次锁定它,所以不是两个线程同时绘制破坏一切。最后,我再次将 _graphIsDrawing 设置为 false,因此我可以再次调用它。
推荐阅读
- mongodb - 在 Mongodb 和 Postgresql 中批量插入
- ios - 改变视图的约束
- laravel - Pikaday 在 laravel livewire 中没有定义
- node.js - 没有用户的图形 api 请求令牌
- xamarin.forms - XAMARIN 通过 PRISM 在 Tabpage 中选择选项卡
- ubuntu - Ubuntu 服务器安装在目标中的 curtin 命令处停止
- python - 使用产品和变体填充电子商务表
- javascript - 如何在mousemove上获取屏幕上像素的颜色?
- linux - 在 Linux Mint 20.1 / Ubuntu 20.04 上安装/卸载 Docker 后网络连接断开
- google-apps-script - 我拥有几个带有按钮激活脚本的 GS 电子表格。我可以授予用户使用它们的权限,而这些用户不会牺牲他们的安全性吗?