Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Async Procedure Call 1

Status
Not open for further replies.

Narizz28

MIS
Mar 8, 2001
161
0
0
Hi gang,

I'm a little stuck. First off let me say that I have absolutely no idea what I'm doing in VB.Net (ok, I know a little, a VERY little).

I'm trying to do a call to a Sub that does a System.Net.DNS.GetHostEntry to return a PTR record of a passed in IP address. I have it working so far thanks to the wonderful internet, but have hit a wall with one aspect of the lookup. I need it to be asynchronous of the form, i.e. leave control at the form while the lookup happens behind the scene. This way, if there a dealy in the lookup, it doesn't prevent the user from doing other things on the form.

Here's what I have so far:

Code:
Public Class Form1

Delegate Sub MySubDelegate(ByVal x As String)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

  Dim ThisIPAddress As String = "1.2.3.4"

  Dim ghn As MySubDelegate = AddressOf fGetHostName

  'This is the call I am trying to do Ansychronously, so it doesn't lock up the rest of the form while it runs.

  ghn.Invoke(ThisIPAddress)

End Sub

Public Sub fGetHostName(ByVal varAddress As String)

  Dim varHostName As String

  Threading.Thread.Sleep(10000)

  varHostName = System.Net.Dns.GetHostEntry(varAddress).HostName

  If varHostName = varAddress Then
    PTRLabel.Text = "Could not resolve address!"
  Else
    PTRLabel.Text = "PTR Record is " & varHostName
  End If

End Sub

Private Sub btnIncrement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIncrement.Click

    Dim Count As Integer

    Count = Label1.Text

    Count += 1

    Label1.Text = Count

  End Sub

End Class

This code validates without error, but doesn't return control until the called sub finishes. Obviously I either need to go back classic VB, stop trying to be a programmer (actually I'm not trying to be, I'm a sysadmin, but I'm trying my hand at a project), or turn to a marvelous life of digging ditches and forfiet to horrible defeat.

I prefer to get this figured out, but realize I need help.

More about the history so far can be found at:


I look forward to seeing where I went wrong.

Thank in advance for your help.

Narizz
 
the threading sleep will cause the unresponsivness.

What you need is a thread.

But because you have a thread you need a delegate to update the main thread. the main thread being the one that draws the text on the controls.

Code:
Imports System.Threading

Public Class Form1

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

    End Sub

    Delegate Sub MySubDelegate(ByVal s As String)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim ThisIPAddress As String = "127.0.0.1"

        Dim ghn As Thread = New Thread(AddressOf fGetHostName)
        ghn.Start(ThisIPAddress)
    End Sub

    Public Sub fGetHostName(ByVal varAddress As String)
        setPTRLAbel("Resolving...")
        Dim varHostName As String
        Thread.CurrentThread.Sleep(5000)
        varHostName = System.Net.Dns.GetHostEntry(varAddress).HostName

        If varHostName = varAddress Then
            setPTRLAbel("Could not resolve address!")
        Else
            setPTRLAbel("PTR Record is " & varHostName)
        End If

    End Sub

    Private Sub setPTRLAbel(ByVal s As String)
        If Me.PTRLabel.InvokeRequired Then
            Dim d As New MySubDelegate(AddressOf setPTRLAbel)
            Me.Invoke(d, New Object() {s})
        Else
            PTRLabel.Text = s
        End If
    End Sub

    'here's where it gets messy. and where the syntax could be wrong, my bad.
    Private Sub btnIncrement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIncrement.Click
        Dim Count As Integer
        Integer.TryParse(Label1.Text, Count)
        
        Count += 1

        Label1.Text = Count.ToString
    End Sub

End Class

this was fun.

Christiaan Baes
Belgium

My Blog
 
Christiaan,

YOU ROCK! That did the trick perfectly. Thanks for your assistance with this.

Narizz
 
BTW you can leave out the sleep thing ;-).

Thanks for the purple thing.

Christiaan Baes
Belgium

My Blog
 
Yeah, I did. I put it in my project and guess what, it worked like a champ. I decided to change the BG and FG colors depending on the result. Red BG with White FG if failed, and Green BG with Black FG if success. It works perfectly.

I can't help myself, if I can, I'm going to give another purple thingy.

Narizz
 
Dang, it won't let me give you another purple thingy. Here's 5 more:

* * * * *


Narizz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top