Hi! My assignment was to create a program to retrieve the IP address based on a host name... which I totally did :) at least I think i did; it does actually work :)

but in my operating systems class the professor introduced threads, and I don't really get it at all, so I thought I would try and combine the two concepts and create a progress bar that reports how much of the lookup is done... but I can't seem to get it to work! here is the code I've set up so far:

Imports System.Net
Public Class Project1

    Private localIP As String

    ''' <summary>
    ''' Handles the Click event of the btnGo control.
    ''' Uses the backWorker process to look up the IP address for a given host name
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="T:System.EventArgs" /> instance containing the event data.</param>
    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        ' Make sure user enters a value
        If hostNameComboBox.Text = "" Then
            ' Reject blank entries
            statusLabel.Text = "Please enter a hostname!"
        Else
            ' disable buttone while processing and begin execution
            btnGo.Enabled = False
            statusLabel.Text = "Working..."
            backWorker.RunWorkerAsync(hostNameComboBox.Text)
        End If

    End Sub

    ''' <summary>
    ''' Handles the DoWork event of the backWorker control.
    ''' Fesolves a given hostname passed through the e parameter into its IP address.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="T:System.ComponentModel.DoWorkEventArgs" /> instance containing the event data.</param>
    Private Sub backWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backWorker.DoWork
backWorker.ReportProgress(0)
        Try
            ' Attempt to resolve the IP address
            localIP = Dns.GetHostEntry(e.Argument).AddressList(0).ToString

            ' If the host name does not exist, inform the user through the result string
        Catch ex As Sockets.SocketException
            localIP = ex.Message
        End Try
backWorker.ReportProgress(100)
    End Sub

    ''' <summary>
    ''' Handles the RunWorkerCompleted event of the backWorker control.
    ''' Displays the result of the IP address lookup in a messagebox.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="T:System.ComponentModel.RunWorkerCompletedEventArgs" /> instance containing the event data.</param>
    Private Sub backWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles backWorker.RunWorkerCompleted
        statusLabel.Text = "Done!"
        MessageBox.Show(localIP, "IP Address", MessageBoxButtons.OK)
        btnGo.Enabled = True
    End Sub

    ''' <summary>
    ''' Handles the Load event of the Project1 control.
    ''' Sets input focus to the prefilled combobox for user input of the hostname.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="T:System.EventArgs" /> instance containing the event data.</param>
    Private Sub Project1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.hostNameComboBox.SelectAll()
        Me.hostNameComboBox.Focus()
    End Sub

    ''' <summary>
    ''' Handles the ProgressChanged event of the backWorker control.
    ''' Updates the progressBar based on percentage of job done.
    ''' </summary>
    ''' <param name="sender">The source of the event.</param>
    ''' <param name="e">The <see cref="T:System.ComponentModel.ProgressChangedEventArgs" /> instance containing the event data.</param>
    Private Sub backWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles backWorker.ProgressChanged
        progressBar.Value = e.ProgressPercentage
    End Sub
End Class

but I can't figure out how to get the progress bar to update based on the backworkers progress... i looked at some sample code online and it seems you have to manually call a reportprogress to the process with the current status, but my backworker only executes one thing...

so can I not do it for this particular application? the backworker process does take some time, especially if the hostname doesn't exist (the catch section)... but am I doing this all wrong? can I not do what I'm trying to do?

thank you in advance for your help!

-SelArom

Hi SelArom,

I made a form with those components and just pasted yur code in and it worked for me.

I'm just trying to learn this backgroundworker stuff and I tried your code before I read that you had a problem - lol

Zach

hi!
i've solved the problem: the only thing you have to add is:

BackWorker.WorkerReportsProgress = True

that's it!

davidps
(i really don't know if someone need it now...)

This article has been dead for over six months. Start a new discussion instead.