c# – Thread.Sleep(1);对于Silverlight动画来说太慢了,还有其他
发布时间:2020-12-15 20:49:40 所属栏目:百科 来源:网络整理
导读:我有这个算法在动画中的Canvas上绘制了很多像素.但是我无法控制动画的速度并且绘制得非常快,所以我添加了一个Thread.Sleep(1)但是当绘制几千个像素时它太慢了. 我尝试了一个故事板方法但最终变得非常缓慢. 那么还有Thread.sleep的替代方案来减慢我的循环吗?
我有这个算法在动画中的Canvas上绘制了很多像素.但是我无法控制动画的速度并且绘制得非常快,所以我添加了一个Thread.Sleep(1)但是当绘制几千个像素时它太慢了.
我尝试了一个故事板方法但最终变得非常缓慢. 那么还有Thread.sleep的替代方案来减慢我的循环吗? void DrawGasket(object sender,DoWorkEventArgs e) { Random rnd = new Random(); Color color = Colors.White; while (Counter <= noOfPx) { switch (rnd.Next(3)) { case 0: m_lastPoint.X = (m_top.X + m_lastPoint.X)/2; m_lastPoint.Y = (m_top.Y + m_lastPoint.Y)/2; color = Colors.White; break; case 1: m_lastPoint.X = (m_left.X + m_lastPoint.X)/2; m_lastPoint.Y = (m_left.Y + m_lastPoint.Y)/2; color = Colors.Orange; break; case 2: m_lastPoint.X = (m_right.X + m_lastPoint.X)/2; m_lastPoint.Y = (m_right.Y + m_lastPoint.Y)/2; color = Colors.Purple; break; } Dispatcher.BeginInvoke(() => { var px = new Rectangle { Height = m_pxSize,Width = m_pxSize,Fill = new SolidColorBrush(color) }; Canvas.SetTop(px,m_lastPoint.Y); Canvas.SetLeft(px,m_lastPoint.X); can.Children.Add(px); lblCounter.Text = Counter.ToString(); }); Counter++; Thread.Sleep(1); } } 解决方法
难道你不能只通过循环而不是每次迭代睡眠N次迭代吗?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |