title: async/await on many forms ping function
body: <p>I have some problem and couldn't see so far what i am doing wrong. My application user can open some form multiple times and within each of it i want to make ping time to time to some remote host then show status on status bar, therefore to not block main thread and ui i decided to go <code>asyn/await</code>. Unfortunetly within <code>'DoWork'</code> method <code>'StartNew'</code> is highlited within this line:</p>
  
  <pre><code>Await Task.Factory.StartNew(IsDestinationReachable(IPAddress))
  </code></pre>
  
  <blockquote>
    <p>Severity  Code    Description Project File    Line    Suppression State
    Error BC36645 Data type(s) of the type parameter(s) in method 'Public
    Overloads Function StartNew(Of TResult)([function] As Func(Of
    TResult)) As Task(Of TResult)' cannot be inferred from these
    arguments. Specifying the data type(s) explicitly might correct this
    error.</p>
  </blockquote>
  
  <p>And by the way is it right way i am doing it? Check my code below:</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>EDIT:</strong>
  i think i was able to make it happen - one thing which still considering me is why when user opening new form before it show up its about 4 seconds... second thing is it correct - i mean can you check my code is there any wrong task conception usage? Generally working...</p>
  
  <pre><code>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
  </code></pre>