async / await在很多表单上ping函数

时间:2016-01-26 19:28:02

标签: vb.net

我有一些问题,到目前为止还看不到我做错了什么。我的应用程序用户可以多次打开某个表单,并且在每个表单中我想对某个远程主机进行ping时间,然后在状态栏上显示状态,因此不阻止主线程,我决定转到asyn/await'DoWork'方法'StartNew'中的不幸内容在这一行中很高兴:

Await Task.Factory.StartNew(IsDestinationReachable(IPAddress))
  

严重级代码描述项目文件行抑制状态   错误BC36645方法'公共中类型参数的数据类型   重载函数StartNew(Of Traesult)([function] As Func(Of。)   TResult))作为任务(TResult)'无法从这些推断出来   参数。明确指定数据类型可能会纠正此问题   错误。

顺便问一下,我这样做是对的吗?检查下面的代码:

Public Class FrmDrukujEtykiete

    Private Property IPAddress As String
    Private WithEvents mytimer As Timer

    Sub New(ipaddress As String)
        InitializeComponent()
        AddHandler mytimer.Tick, AddressOf dowork
        mytimer.Interval = 6000
        mytimer.Enabled = True
        mytimer.Start()
        Me.IPAddress = ipaddress
    End Sub

    Public Async Sub dowork(sender As Object, e As EventArgs)
        Dim MMOO As Task(Of Boolean) = Await Task.Factory.StartNew(IsDestinationReachable(IPAddress))
        If MMOO.Result Then
            tsPingResultIcon.BackColor = Color.Green
            tsPingResultIcon.Text = "OK - Remote ip reachable"
        Else
            tsPingResultIcon.BackColor = Color.Red
            tsPingResultIcon.Text = "NOT OK - Remote ip NOT reachable"
        End If
    End Sub

    Public Function IsDestinationReachable(ByVal hostnameOrAddress As String) As Boolean
        Dim reachable As Boolean = False
        Try
            reachable = My.Computer.Network.IsAvailable AndAlso My.Computer.Network.Ping(hostnameOrAddress)

        Catch pingException As System.Net.NetworkInformation.PingException
        Catch genericNetworkException As System.Net.NetworkInformation.NetworkInformationException

            ' Fail silently and return false
        End Try
        Return reachable
    End Function

End Class

修改 我想我能够实现它 - 还有一件事仍在考虑我是为什么当用户在显示它大约4秒之前打开新表格...第二件事是正确的 - 我的意思是你可以检查我的代码是否有任何错误的任务概念用法?一般工作......

Public Class FrmDrukujEtykiete

    Private etykieta As New Etykieta
    Private Property IPAddress As String
    Private WithEvents mytimer As New Timer

    Sub New(ipaddress As String)
        InitializeComponent()
        Me.IPAddress = ipaddress
        AddHandler mytimer.Tick, AddressOf dowork
        mytimer.Interval = 6000
        mytimer.Enabled = True
        mytimer.Start()
        DoSomethingElse()
    End Sub

    Private Sub DoSomethingElse()
        For i = 1 To 100000
            '  Threading.Thread.Sleep(1000)
            ListBox1.Items.Add(i)
        Next
    End Sub

    Private Sub btnWyjdz_Click(sender As Object, e As EventArgs) Handles btnWyjdz.Click
        Close()
    End Sub

    Public Async Sub dowork(sender As Object, e As EventArgs)

        Dim tsk As Task(Of Boolean) = Task.Factory.StartNew(Of Boolean)(Function()
                                                                            '--Run lenghty task
                                                                            Dim reachable = False
                                                                            Try
                                                                                reachable = My.Computer.Network.IsAvailable AndAlso My.Computer.Network.Ping(IPAddress)

                                                                            Catch pingException As System.Net.NetworkInformation.PingException
                                                                            Catch genericNetworkException As System.Net.NetworkInformation.NetworkInformationException

                                                                                ' Fail silently and return false
                                                                            End Try
                                                                            Return reachable
                                                                        End Function)

        Await tsk

        ListBox2.Items.Add("a teraz zmiana")

        If tsk.Result Then
            tsPingResultIcon.BackColor = Color.Green
            tsPingResultIcon.Text = "OK - Remote ip reachable"
        Else
            tsPingResultIcon.BackColor = Color.Red
            tsPingResultIcon.Text = "NOT OK - Remote ip NOT reachable"
        End If

    End Sub

    Public Function IsDestinationReachable(ByVal hostnameOrAddress As String)
        Dim reachable = False
        Try
            reachable = My.Computer.Network.IsAvailable AndAlso My.Computer.Network.Ping(hostnameOrAddress)

        Catch pingException As System.Net.NetworkInformation.PingException
        Catch genericNetworkException As System.Net.NetworkInformation.NetworkInformationException

            ' Fail silently and return false
        End Try
        Return reachable
    End Function



End Class

1 个答案:

答案 0 :(得分:0)

您的更新代码通常不会使用Async / Await。更常见的是:

Public Async Sub dowork(sender As Object, e As EventArgs)
    Dim isReachable As Boolean = Await Task.Factory.StartNew(Function() IsDestinationReachable(IPAddress))
    If isReachable Then
        tsPingResultIcon.BackColor = Color.Green
        tsPingResultIcon.Text = "OK - Remote ip reachable"
    Else
        tsPingResultIcon.BackColor = Color.Red
        tsPingResultIcon.Text = "NOT OK - Remote ip NOT reachable"
    End If
End Sub

Await挂起方法,直到任务完成,然后返回任务的结果,因此无需保留Task对象并使用.Result。当该方法被挂起时,非Await代码的执行在主线程上继续,并且当异步方法完成时,该方法在主线程上恢复。

但是,由于网络IO自然是异步的,因此最好使用Ping.SendPingAsync方法而不是Task.Factory.StartNew。这将是这样的(未经测试的代码):

Public Async Sub dowork(sender As Object, e As EventArgs)
    Dim isReachable As Boolean = Await IsDestinationReachable(IPAddress)
    If isReachable Then
        tsPingResultIcon.BackColor = Color.Green
        tsPingResultIcon.Text = "OK - Remote ip reachable"
    Else
        tsPingResultIcon.BackColor = Color.Red
        tsPingResultIcon.Text = "NOT OK - Remote ip NOT reachable"
    End If
End Sub

Public Async Function IsDestinationReachable(ByVal hostnameOrAddress As String) As Boolean
    Dim reachable As Boolean = False
    Try
        If My.Computer.Network.IsAvailable Then
            Dim pinger As Ping = New Ping()
            Dim result As PingReply = Await pinger.SendPingAsync(hostnameOrAddress) 
            reachable = result.Status = IPStatus.Success
        End If
    Catch pingException As System.Net.NetworkInformation.PingException
    Catch genericNetworkException As System.Net.NetworkInformation.NetworkInformationException
        ' Fail silently and return false
    End Try
    Return reachable
End Function