Threading

Skip all sorts of Safe Threading calls

Me.CheckForIllegalCrossThreadCalls = False  

Threading a Sub with Params!

Private Sub Btn_Click()

Dim evaluator As New Thread(Sub() Me.testthread(goodList, 1))

evaluator.Start()

Exit Sub


This is the testthread method:


Private Sub testthread(ByRef goodList As List(Of OneItem), ByVal coolvalue As Integer)

    StatusProgressBar.Maximum = 100000

    While (coolvalue < 100000)

        coolvalue = coolvalue + 1

        StatusProgressBar.Value = coolvalue

        lblPercent.Text = coolvalue & "%"

        Me.StatusProgressBar.Refresh()

    End While

End Sub

https://stackoverflow.com/questions/4018282/how-to-pass-multiple-parameters-in-thread-in-vb

System Timer (threading them)


'http://www.vbforums.com/showthread.php?469362-RESOLVED-2005-system-timers-timer-no-tag-property

Public Class myNewTimer

    Inherits System.Timers.Timer

    Private _IDNumber As String

    Property IDNumber() As String

        Get

            Return _IDNumber

        End Get

        Set(ByVal value As String)

            _IDNumber = value

        End Set

    End Property

End Class

    Sub StartTimer(sComputer As String)

        Dim oTimer As New myNewTimer

        'Dim oTimer As New System.Threading.Timer()

        AddHandler oTimer.Elapsed, AddressOf Timer_Tick

        oTimer.IDNumber = sComputer

        oTimer.Interval = 1000

        oTimer.Start()

        ' Do While Not bStop

        ' Application.DoEvents()

        '  Loop

    End Sub

  Sub StartAll()

        For Each sServer In {"server1","server2",…}

            Debug.Print(sServer)

            'Dim evaluator As New Threading.Thread(Sub() StartTimer(sServer))

            Dim evaluator As New Threading.Thread(Sub() StartTimer(sServer))

            evaluator.Start()

            iThreads += 1

            ' StartTimer("vs12app01")

            'StartTimer(".")

            ' StartTimer(sServer)

        Next

        While iThreads > 0 And Not bStop

            Application.DoEvents()

        End While

        bFirstRun = False

     

    End Sub

   Private Sub Timer_Tick(sender As Object, e As EventArgs)

        Dim instance As myNewTimer = DirectCast(sender, myNewTimer)

        instance.Stop()

        Debug.Print(Now & "  Tick for: " & instance.IDNumber.ToString)

   End Sub

Wait for Threads to finish*


* may want to add a time function so it's not endless

While oThreads.Count > 0
                oThreads.Remove(oThreads.Find(Function(item2 As Threading.Thread) Not item2.ThreadState = Threading.ThreadState.Running))
                Application.DoEvents()
End While

Cycle through a List(of threads) to abort and remove from list           

Dim aThrds As New List(Of Thread) For Each oT As Thread In oThreads aThrds.Add(oT) Next For Each oThrd As Threading.Thread In aThrds oThrd.Abort() oThreads.Remove(oThrd) Next

Get previous control

'https://stackoverflow.com/questions/38869180/get-last-active-control-with-activecontrol-or-with-any-method-and-sendkeys-to
Private PreviousControl As Control

Private Sub doSet(ByVal parentCtr As Control)
    Dim ctr As Control
    For Each ctr In parentCtr.Controls
        AddHandler ctr.Leave, AddressOf meLeave
        doSet(ctr)
    Next
End Sub

Private Sub meLeave(ByVal sender As Object, ByVal e As System.EventArgs)
    PreviousControl = DirectCast(sender, Control)
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    doSet(Me)
End Sub

Private Sub btn15_Click(sender As Object, e As EventArgs) Handles btn1.Click
    Dim strName As String
    strName = PreviousControl.Name
End Sub

Update control via Delegate Invoke

Private Delegate Sub SetTextBoxTextInvoker(text As String)
Sub SetLabelText(ByVal text As String)
'or me.label1, form1.label1 or anything else I can try!
    If Me.InvokeRequired Then
        Me.Invoke(New SetTextBoxTextInvoker(AddressOf SetLabelText), _
               text)
    Else
        Me.Label1.Text = text
    End If
End Sub
End Class

Picturebox with color and width Example


Private Delegate Sub SetpictNameAndStatusInvoker(text As String, oColor As Color, oPict As PictureBox, iWidth As Integer)

    Sub SetLabelText(text As String, oColor As Color, oPict As PictureBox, iWidth As Integer)
        'or me.label1, form1.label1 or anything else I can try!
        If Me.InvokeRequired Then
            Me.Invoke(New SetpictNameAndStatusInvoker(AddressOf SetLabelText),
            text, oColor, oPict, iWidth)
        Else
            oPict.Image = createTextImage(text, oColor, iWidth)
        End If

    End Sub

Tooltip Invoke


    Private Delegate Sub SetpictTooltipInvoker(text As String, oPict As PictureBox)

    Sub SetTTtext(text As String, oPict As PictureBox)
        'or me.label1, form1.label1 or anything else I can try!
        If Me.InvokeRequired Then
            Me.Invoke(New SetpictTooltipInvoker(AddressOf SetTTtext),
            text, oPict)
        Else
            'oPict.Image = createTextImage(text, oColor, iWidth)
            Dim bFound As Boolean = False

            'For Each oControl As Control In oPict.Controls
            '    If TypeOf oControl Is ToolTip Then


            '    End If
            'Next
            'Dim tt As New ToolTip
            tt.RemoveAll()
            tt.SetToolTip(oPict, text)

        End If

    End Sub

Save Dialog Invoke Example

    Private Delegate Function SetOSaveDialog() As String
    Function SetSaveAsDiaglogBox() As String
        'or me.label1, form1.label1 or anything else I can try!
        If Me.InvokeRequired Then
            Return Me.Invoke(New SetOSaveDialog(AddressOf SetSaveAsDiaglogBox))
        Else
            If (SaveFileDialog1.ShowDialog = DialogResult.OK) Then
                Return "" & SaveFileDialog1.FileName & IIf(Microsoft.VisualBasic.Strings.Right(LCase(SaveFileDialog1.FileName), 4) <> ".pdf", ".pdf", "")


            Else
                Return ""
            End If
        End If
    End Function

Get ListView Item by Index Invoke Example

    Private Delegate Function GetListItemsInvoker(LV As ListView, x As Integer) As ListViewItem
    Function GetLVitems(ByVal LV As ListView, x As Integer) As ListViewItem
        'or me.label1, form1.label1 or anything else I can try!
        If Me.InvokeRequired Then
            Return Me.Invoke(New GetListItemsInvoker(AddressOf GetLVitems),
                   LV, x)
        Else
            Return LV.Items(x)
        End If
    End Function

ListView Item Change Color Invoke Example

    Private Delegate Sub SetListItemColor(ByRef LV As ListViewItem, oColor As Color)
    Sub SetLVcolor(ByRef LV As ListViewItem, oColor As Color)
        'or me.label1, form1.label1 or anything else I can try!
        If Me.InvokeRequired Then
            Me.Invoke(New SetListItemColor(AddressOf SetLVcolor),
                   LV, oColor)
        Else
            LV.BackColor = oColor
            LV.EnsureVisible()
        End If
    End Sub

ListView SubItem Change Text Invoke Example

    Private Delegate Sub SetListSubItem(ByRef LV As ListViewItem.ListViewSubItem, text As String)
    Sub SetLVsubitem(ByRef LV As ListViewItem.ListViewSubItem, text As String)
        'or me.label1, form1.label1 or anything else I can try!
        If Me.InvokeRequired Then
            Me.Invoke(New SetListSubItem(AddressOf SetLVsubitem),
                   LV, text)
        Else
            LV.Text = text
            'LV.EnsureVisible()

        End If
    End Sub

ListItem Change Image Invoke Example

    Private Delegate Sub SetListItemIcon(ByRef LV As ListViewItem, text As String)
    Sub SetLVicon(ByRef LV As ListViewItem, text As String)
        'or me.label1, form1.label1 or anything else I can try!
        If Me.InvokeRequired Then
            Me.Invoke(New SetListItemIcon(AddressOf SetLVicon),
                   LV, text)
        Else
            LV.ImageKey = text
            LV.EnsureVisible()
        End If
    End Sub

ToolStripStatusLabel Invoke Example


    Private Delegate Sub SetToolStripStatusLabelDel(ByRef LV As ToolStripStatusLabel, text As String, icon As String)
    Sub SetToolStripText(ByRef LV As ToolStripStatusLabel, text As String, icon As String)
        'or me.label1, form1.label1 or anything else I can try!
        If Me.InvokeRequired Then
            Me.Invoke(New SetToolStripStatusLabelDel(AddressOf SetToolStripText),
                   LV, text, icon)
        Else
            LV.ImageKey = icon
            LV.Text = text
        End If
    End Sub

SetToolStripProgressBar

    Private Delegate Sub SetToolStripProgressBarDel(ByRef LV As ToolStripProgressBar, iCurrent As Integer, iMax As Integer)
    Sub SetToolStripProgressBar(ByRef LV As ToolStripProgressBar, iCurrent As Integer, iMax As Integer)
        'or me.label1, form1.label1 or anything else I can try!
        If Me.InvokeRequired Then
            Me.Invoke(New SetToolStripProgressBarDel(AddressOf SetToolStripProgressBar),
                   LV, iCurrent, iMax)
        Else
            If iMax = 0 Then LV.Visible = False Else LV.Visible = True
            LV.Maximum = iMax
            If iCurrent >= 0 Then LV.Value = iCurrent Else LV.Value = 0

            LV.Size = New Size(New System.Drawing.Point(500))
        End If
    End Sub