我经常遇到计时器问题。有时我可以同时运行10个定时器而没有问题,其他2个定位空方法就足以让一切都冻结。
引爆点是,现在我甚至无法在一个级别上运行单个计时器,其优先级高于背景。计时器用于启用在画布周围拖动使用元素,并将其优先级设置为渲染使其比在背景中更平滑。
当我在计时器的事物列表中添加一个新子时,它现在会冻结高于背景的任何更高的piority。我添加的子应该计算元素之间的位置对齐,没有重载,每个元素的少量计算。该子实际上位于它所调用的类之外(在canvas类中),如果这有所不同。
我使用的计时器类是windows.threading.dispatchtimer。从我的红色开始,它运行主要的ui线程,这是我需要的,因为这是它需要操纵的东西。虽然winforms中的计时器没有这样的麻烦,即使工作量更大,所以给出了什么呢?
下面是相关内容的一些代码片段:
floater class:
Private WithEvents transformTimer As New Windows.Threading.DispatcherTimer(Windows.Threading.DispatcherPriority.Render, Dispatcher) With {.Interval = New TimeSpan(166)}
Then there is a sub that handles click on the floater object and enabled the timer
then there is a sub that handles the timer ticks, checks for mouse buttons and position to make the necessary adjustments and calls the following sub
Public Sub place(r As DoubleRect)
If mainTitle.snap Then
mouseSnap = wParent.getMoveSnap(Me)
wParent.writeAll(mouseSnap.ToString)
sRect.x = mouseSnap.X
sRect.y = mouseSnap.Y
End If
If cRect.x <> r.x Then Canvas.SetLeft(Me, r.x + sRect.x)
If cRect.y <> r.y Then Canvas.SetTop(Me, r.y + sRect.y)
If cRect.w <> r.w Then Me.Width = r.w + sRect.w
If cRect.h <> r.h Then Me.Height = r.h + sRect.h
cRect = r.clone
vRect = rActualToVisible(cRect)
End Sub
DoubleRect is a basic rectangle class with doubles for location and size
and then there is the sub that makes the whole thing freeze in combination with the timer priority
Public Function getMoveSnap(rWindow As uiWindow) As Point
Dim vRect As DoubleRect = rWindow.vRect
Dim vRect2 As DoubleRect
Dim dir As sDirections
Dim amt As Double
For i = 0 To rWindows.Count - 1
If Not rWindows(i).Equals(rWindow) Then
vRect2 = rWindows(i).vRect
dir = vRect.sDirection(vRect2)
If dir = sDirections.N Then
amt = vRect.y - vRect2.y2
If amt < snapDistance Then Return New Point(0, -amt)
ElseIf dir = sDirections.S Then
amt = vRect2.y - vRect.y2
If amt < snapDistance Then Return New Point(0, amt)
ElseIf dir = sDirections.W Then
amt = vRect.x - vRect2.x2
If amt < snapDistance Then Return New Point(-amt, 0)
ElseIf dir = sDirections.E Then
amt = vRect2.x - vRect.x2
If amt < snapDistance Then Return New Point(amt, 0)
Else
Return New Point()
End If
End If
Next
End Function
sDirections is an enum of N S W E and invalid(X) directions to snap in
sDirection is a method of doublerect that does a long chain of "if x > r.x then blah blah" to determine the direction