| | |
Understanding the BackgroundWorker Process
Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
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
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:
VB.NET Syntax (Toggle Plain Text)
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
You are somebody, just as I am somebody... but in the end, when you REALLY think about it, we are all nobody...
•
•
Join Date: Mar 2009
Posts: 1
Reputation:
Solved Threads: 0
hi!
i've solved the problem: the only thing you have to add is:
that's it!
davidps
(i really don't know if someone need it now...)
i've solved the problem: the only thing you have to add is:
VB.NET Syntax (Toggle Plain Text)
BackWorker.WorkerReportsProgress = True
davidps
(i really don't know if someone need it now...)
![]() |
Similar Threads
- Tutorial: Understanding ASP classes (ASP)
- As a newbie, where i should start from in linux? (Getting Started and Choosing a Distro)
- Can't save audio with my avi files (Windows NT / 2000 / XP)
- "System Idle Process" (Windows NT / 2000 / XP)
- Kill A Process (Windows NT / 2000 / XP)
- Regarding ASP.NET worker process (ASP.NET)
- Program design tools (C++)
Other Threads in the VB.NET Forum
- Previous Thread: add a decimal point
- Next Thread: How do I link comboboxes together?
Views: 25919 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2005 2008 access account application array arrays basic bing button buttons c# center check checkbox code convert crystalreport data database datagrid datagridview date design designer dissertation dissertations dropdownlist excel fade file-dialog filter ftp generatetags google gridview hardcopy images inline input insert installer intel internet listview mobile monitor net networking objects output panel passingparameters picturebox port position print printing problem read remove save searchbox searchvb.net select serial shutdown soap sorting studio survey table tcp temperature text textbox time timer timespan toolbox trim update user validation vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet visual visualbasic visualbasic.net visualstudio2008 web webbrowser winforms wpf year





