我在VB.NET中遇到了一些多线程编程的问题,我看不出原因。在我正在编写的程序的一部分中,我希望点(黄色椭圆)以xInterval毫秒的设定间隔定期出现在面板对象上,并在显示xFlashtime毫秒后再次消失(用黑色椭圆绘制)。我的代码是:
Sub sparkles()
...
'pointArray() contains the points where the dots are to appear
For i as Integer = 0 to pointArray.Length - 1
flashPoint(pointArray(i))
Threading.Thread.Sleep(xInterval)
Next i
End Sub
Async Sub flashPoint(point As Point) As Task
*CODE TO DRAW YELLOW ELLIPSE AT point*
Await killPoint(point)
End Sub
Function killPoint(point As Point) As Task
Threading.Thread.Sleep(xFlashtime)
*CODE TO DRAW BLACK ELLIPSE AT point*
End Function
我想要发生的是新线程每隔xInterval毫秒运行一次flashPoint实例,然后该线程运行killPoint(它会在xFlashtime毫秒之后将该点黑掉)并在主线程继续通过For时死掉循环。
我应该在For循环中将flashPoint(point)作为任务调用吗?或者我是否需要使用Threading.Threads创建定时监听器(希望避免使用优雅的“Async”)?