【发布时间】:2015-01-12 00:32:23
【问题描述】:
我有一个 C# 应用程序(winforms 和 wpf,但对于这个问题,我将重点放在 winforms 上),其中 backgroundWorker 用于处理数据集,并调用 ProgressChanged 然后调用表单 Refresh 方法强制重绘。然后根据数据集的当前帧绘制一堆椭圆。
一个给定的帧可能涉及绘制零到几百个椭圆之间的任何地方。
此外,我还有一个滑块控件,允许用户调整播放速率(基本上是循环内的 thread.sleep 值。)
当用户将睡眠值设置得太低时,有时重绘方法会排队,并且 UI 变得无响应。 (这取决于帧中椭圆的数量,以及计算机的速度。并且在 UI 上重新绘制时延迟是 100%,而不是任何其他处理,这基本上只是增加一个计数器并设置一个标签文本.)
我希望能够检测到排队并自动调整速度滑块以适应更大的数据集和/或速度较慢的计算机。如何判断 UI 线程是否通过多次调用 Map_Paint 进行备份?
当前代码(释义):
public Map()
{
InitializeComponent();
_worker = new BackgroundWorker();
_worker.DoWork += _worker_DoWork;
_worker.ProgressChanged += _worker_ProgressChanged;
_worker.WorkerReportsProgress = true;
}
private void _worker_DoWork(object sender, DoWorkEventArgs e)
{
_frameCount = _frames.FrameCount();
// For this specific example, _frameCount may be around 30000-40000
for (var i = 0; i < _frameCount; i++)
{
var f = _frames.Frame(i + 1);
_worker.ReportProgress(i, f);
Thread.Sleep(_tickCount);
_suspend.WaitOne(); // Used to Pause the playback
}
}
void _worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// set some variables according to state and progresspercentage snipped here
// ...
// I would like to detect at this point whether the screen repainting is backed up
// and if so, adjust the value of _tickCount to slow down the program.
this.Refresh();
}
private void Map_Paint(object sender, PaintEventArgs e)
{
// Lots of ellipsis drawing stuff here
// Maybe 0-1000 ellipses drawn per cycle.
}
private void tbSpeed_Scroll(object sender, EventArgs e)
{
// This is the Scroll event for the slider.
// Value range is 10-300
// The slider becomes unresponsive when the UI thread backs up.
// I'd like to detect the back up and override the value of _tickCount
_tickCount = tbSpeed.Value;
}
【问题讨论】:
-
解决您所描述的问题的第一步是降低工作线程的线程优先级,以确保 UI 线程(以“正常”优先级运行)将始终获得优先权,如果它有工作要做。注意 WM_PAINT 消息不是真正的消息;当消息队列为空并且有绘制要完成时,Windows 会根据需要生成它们。因此,您实际上无法检查 WM_PAINT 消息是否正在备份(它们从不备份)。您可以使用本机代码检查消息队列长度本身,但恕我直言,这里不需要。
-
好建议彼得。我也试试看。
标签: c# multithreading backgroundworker