I have a winforms application and at times the main thread is extremely busy. When the main thread is busy, I want the user to be able to drag and drop the main form (using the pbAppHeader control at the top of the form), so they can reposition the main form on the screen. Since the main thread is extremely busy at times, moving the main form is very slow and jerky.
How do I run the following subs on a different thread or backgroundworker so moving the main form is smooth? Or is there a better way to do this?
Dim drag As Boolean
Dim mousex As Integer
Dim mousey As Integer
Private Sub pbAppHeader_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbAppHeader.MouseDown
drag = True 'Sets the variable drag to true.
mousex = Windows.Forms.Cursor.Position.X - Me.Left 'Sets variable mousex
mousey = Windows.Forms.Cursor.Position.Y - Me.Top 'Sets variable mousey
End Sub
Private Sub pbAppHeader_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbAppHeader.MouseMove
'If drag is set to true then move the form accordingly.
If drag Then
Me.Top = Windows.Forms.Cursor.Position.Y - mousey
Me.Left = Windows.Forms.Cursor.Position.X - mousex
End If
End Sub
Private Sub pbAppHeader_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbAppHeader.MouseUp
drag = False 'Sets drag to false, so the form does not move according to the code in MouseMove
End Sub