1

I'm having constant trouble with timers. Sometimes i can run 10 timers concurrently with no problem, other times 2 that are targeting empty methods are enough to make everything freeze.

The tipping point is that now I cant even run a single timer on one class with a priority any higher than background. The timer is used for enabling dragging userelements around a canvas and setting its priority to render makes it way smoother than it is in background.

When i added a new sub to the list of things for the timer to do though, it now freezes on any higher piority higher than background. The sub I added is supposed to calculate positional snapping between the elements, nothing heavy duty, a handful of calculations per element. The sub does reside outside the class where its called from though (in the canvas class) if that makesa difference.

The timer class I'm using is windows.threading.dispatchtimer. From what i've red about it, it runs off the main ui thread which is what i need because thats what it needs to manipulate. I had no such trouble with timers in winforms though, even with much beefier workloads so what gives?

Heres some code snippets of the relevant stuff:

 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
Alex
  • 4,821
  • 16
  • 65
  • 106
Jake Freelander
  • 1,471
  • 1
  • 19
  • 26

0 Answers0