Question [Express 2008] While loop without freezing?

littlebigman

Well-known member
Joined
Jan 5, 2010
Messages
75
Programming Experience
Beginner
Hello,

I'm a VB.Net newbie, and would like to write a small app that runs a While loop to check a DNS domain name, and exits once the domain has been registered.

I found code at How to Get Host Name and IP Address in VB.Net - .Net Articles & Samples to resolve the hostname and handle the "No such host is known" error if it still hasn't been registered, but I have a couple of issues:

1. How to make sure the GUI is correctly displayed even though there's an endless While loop? Should I use some kind of asynchronous call so that the window doesn't freeze?

2. I'd like the app to mimize to the icon bar, and have its window title display some status information ("Still waiting/Registered!") so that I can simply move the mouse over it and know whether the domain has finally been registered or not.

Thank you.
 
Made some progress: To avoid freezing he UI, we must use the BackgroundWorker object.

At this point, I get an error when trying to change the form's title. Does someone know how to call the ProgressChanged routine from DoWork()?
VB.NET:
Expand Collapse Copy
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        BackgroundWorker1.WorkerReportsProgress = True

        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        While (True)
            Try
                Dim myIPs As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry("www.mydomain.com")

                For Each myIP As System.Net.IPAddress In myIPs.AddressList
                    Me.Text = myIP.ToString
                Next
                Exit While
            Catch ex As Exception
                '{"Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on."}
                'Me.Text = ex.Message

                'How to call BackgroundWorker1_ProgressChanged?
            End Try

            System.Threading.Thread.Sleep(2000)
        End While
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        MessageBox.Show("Done!")
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged

    End Sub
 
Thank you. Here's how to pass the error message to the ProgressChanged() event, and change the form's title bar:

VB.NET:
Expand Collapse Copy
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        While (True)
            Try

            Catch ex As Exception
                BackgroundWorker1.ReportProgress(100, ex.Message)
            End Try

            System.Threading.Thread.Sleep(2000)
        End While
    End Sub

    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Me.Text = e.UserState.ToString
    End Sub
 
Back
Top