Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it L'avatar di SigAlexey
    Registrato dal
    May 2014
    residenza
    Treviso
    Messaggi
    186

    [VB.NET] Freeze del programma su "ping" multithreading

    Buongiorno, come da titolo avrei bisogno di una soluzione per il freeze causato dal ping.

    Mi spiego meglio, devo controllare la connessione con un dispositivo in rete.
    Ora uso questo metodo, ma non funziona correttamente:

    All'interno del Timer1:

    codice:
     If t_checkPing.IsAlive = False Then
                t_checkPing = New Threading.Thread(AddressOf checkPing)
                t_checkPing.Start("192.168.59.39")
            End If

    E successivamente:

    codice:
    Dim t_checkPing As New System.Threading.Thread(AddressOf checkPing)
    
    Sub checkPing(ipAddress As String)
    
                If My.Computer.Network.Ping(ipAddress, 500) = False Then
    
    
                    testoMessaggioAvviso = "COMMUNICATION" & vbCrLf & "ERROR"
    
    
                    Invoke(Sub()
                                       'Interazione con la GUI                          
                           End Sub)
    
    
                Else
    
    
                    Invoke(Sub()
                                'Interazione con la GUI
                           End Sub)
    
    
                End If
    
    End Sub
    Finchè il dispositivo è collegato, funziona tutto, ma non appena il dispositivo viene spento o comunque la connessione non c'è più, ad ogni ciclo in cui vado ad effettuare la verifica, mi si freeza il programma per un paio di secondi.

    Ho trovato nel web qualcosa riguardo il l'AsyncPing, ma non riesco a venirne fuori.
    Sto provando ma mi da sempre lo stesso problema.

    Qualcuno saprebbe aiutarmi?

    Grazie

  2. #2
    Utente di HTML.it L'avatar di SigAlexey
    Registrato dal
    May 2014
    residenza
    Treviso
    Messaggi
    186
    Buongiorno,
    dato che ho trovato la soluzione, la posto nel caso qualcuno abbia bisogno:

    basta creare questa Sub e richiamarla dove ci serve, io l'ho richiamata all'interno di un Timer

    Come parametri gli passeremo l'Ip da pingare, il timeout e i tentativi che dovrà fare.
    Farà tanti tentativi quanti ne faremo fare e alla fine farà la media dei risultati.
    Se come media otterrà un risultato > 0.7 tentativi con successo, allora risponderà "Destination Avaible"

    codice:
    Private Async Sub doPing(ipHost As String, timeout As Integer, Optional tentativi As Integer = 1)
    
            Dim totalPingsToTry As Integer = tentativi
            Dim successCount As Integer
            Me.UseWaitCursor = True
    
    
            Using myPinger As New Ping
                For counter As Integer = 1 To totalPingsToTry
                    Try
                        Dim myReply As PingReply = Await myPinger.SendPingAsync(ipHost, timeout)
                        If myReply.Status = IPStatus.Success Then
                            successCount += 1
                        End If
                    Catch ex As Exception
                        'Ping Failed
                    End Try
                Next
            End Using
    
    
            If successCount / totalPingsToTry >= 0.7 Then
               MsgBox(String.Format("Destination Available - Hit Success Was {0}", (successCount / totalPingsToTry).ToString("P2")))
            Else
                MsgBox(String.Format("Destination Is Not Available - Hit Success Was {0}", (successCount / totalPingsToTry).ToString("P2")))
            End If
    
    
            Me.UseWaitCursor = False
    
    
        End Sub
    Codice preso e modificato da questa conversazione:
    https://www.vbforums.com/showthread....Accuracy-issue

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.