Question Launch Button Event in the background

mac-duff

Member
Joined
Dec 21, 2009
Messages
24
Programming Experience
Beginner
Hello,

I am using a button to check which PCs are online which works fine so far. The problem is that the app is blocked for this time I click on the button and perform this action.

How can I do this in the background?

Thx
VB.NET:
    Private Sub Button_CheckOnline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_CheckOnline.Click
        On Error Resume Next
        If My.Computer.Network.IsAvailable = True Then

            'Dim rowindex As DataGridViewRow

            For Each row As DataGridViewRow In HardwareDataGridView.Rows
                'MessageBox.Show(CStr(row.Cells(DataGridViewTextBoxColumn32.Index).Value))
                If CStr(row.Cells(DataGridViewTextBoxColumn14.Index).Value) > -1 Then

                    If My.Computer.Network.Ping(CStr(row.Cells(DataGridViewTextBoxColumn14.Index).Value), 1000) = True Then
                        'MsgBox("Online")
                        row.Cells(StatusOnline.Index).Style.ForeColor = Color.Green
                        row.Cells(StatusOnline.Index).Value = "Online"

                    Else
                        'MsgBox("Offline")
                        row.Cells(StatusOnline.Index).Style.ForeColor = Color.Red
                        row.Cells(StatusOnline.Index).Value = "Offline"
                    End If
                End If

            Next

        End If
    End Sub
 
To expand on demausdauth's post. You'll want that code to run in a BackgroundWorker's DoWork() event and the button simply starts the the background thread (call's the BW's Start method)

The Completed event fires when it's done.
 
thanks that works perfect:

VB.NET:
Private WithEvents PingWorker As System.ComponentModel.BackgroundWorker

    Private Sub Button_CheckOnline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_CheckOnline.Click
        Button_CheckOnline.Enabled = False
        PingWorker = New System.ComponentModel.BackgroundWorker
        PingWorker.WorkerReportsProgress = True
        PingWorker.WorkerSupportsCancellation = True
        PingWorker.RunWorkerAsync()

    End Sub

    Private Sub PingWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles PingWorker.DoWork
        On Error Resume Next
        If My.Computer.Network.IsAvailable = True Then

            'Dim rowindex As DataGridViewRow

            For Each row As DataGridViewRow In HardwareDataGridView.Rows
                'MessageBox.Show(CStr(row.Cells(DataGridViewTextBoxColumn32.Index).Value))
                If CStr(row.Cells(DataGridViewTextBoxColumn14.Index).Value) > -1 Then
                    If My.Computer.Network.Ping(CStr(row.Cells(DataGridViewTextBoxColumn14.Index).Value), 1000) = True Then
                        'MsgBox("Online")
                        row.Cells(StatusOnline.Index).Style.ForeColor = Color.Green
                        row.Cells(StatusOnline.Index).Value = "Online"

                    Else
                        'MsgBox("Offline")
                        row.Cells(StatusOnline.Index).Style.ForeColor = Color.Red
                        row.Cells(StatusOnline.Index).Value = "Offline"
                    End If
                End If

            Next

        End If
    End Sub
 
On top of modifying the code to either use the ProgressChanged event (You use the .ReportProgress() method to raise the event on the UI thread) or using delegation to update the DGV, I would remove the 'On Error Resume Next' line and use proper error handling instead.
 
Back
Top