Understanding the BackgroundWorker Process

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2004
Posts: 54
Reputation: SelArom is an unknown quantity at this point 
Solved Threads: 1
SelArom's Avatar
SelArom SelArom is offline Offline
Junior Poster in Training

Understanding the BackgroundWorker Process

 
0
  #1
Jan 25th, 2006
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:

  1. Imports System.Net
  2. Public Class Project1
  3.  
  4. Private localIP As String
  5.  
  6. ''' <summary>
  7. ''' Handles the Click event of the btnGo control.
  8. ''' Uses the backWorker process to look up the IP address for a given host name
  9. ''' </summary>
  10. ''' <param name="sender">The source of the event.</param>
  11. ''' <param name="e">The <see cref="T:System.EventArgs" /> instance containing the event data.</param>
  12. Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
  13. ' Make sure user enters a value
  14. If hostNameComboBox.Text = "" Then
  15. ' Reject blank entries
  16. statusLabel.Text = "Please enter a hostname!"
  17. Else
  18. ' disable buttone while processing and begin execution
  19. btnGo.Enabled = False
  20. statusLabel.Text = "Working..."
  21. backWorker.RunWorkerAsync(hostNameComboBox.Text)
  22. End If
  23.  
  24. End Sub
  25.  
  26. ''' <summary>
  27. ''' Handles the DoWork event of the backWorker control.
  28. ''' Fesolves a given hostname passed through the e parameter into its IP address.
  29. ''' </summary>
  30. ''' <param name="sender">The source of the event.</param>
  31. ''' <param name="e">The <see cref="T:System.ComponentModel.DoWorkEventArgs" /> instance containing the event data.</param>
  32. Private Sub backWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles backWorker.DoWork
  33. backWorker.ReportProgress(0)
  34. Try
  35. ' Attempt to resolve the IP address
  36. localIP = Dns.GetHostEntry(e.Argument).AddressList(0).ToString
  37.  
  38. ' If the host name does not exist, inform the user through the result string
  39. Catch ex As Sockets.SocketException
  40. localIP = ex.Message
  41. End Try
  42. backWorker.ReportProgress(100)
  43. End Sub
  44.  
  45. ''' <summary>
  46. ''' Handles the RunWorkerCompleted event of the backWorker control.
  47. ''' Displays the result of the IP address lookup in a messagebox.
  48. ''' </summary>
  49. ''' <param name="sender">The source of the event.</param>
  50. ''' <param name="e">The <see cref="T:System.ComponentModel.RunWorkerCompletedEventArgs" /> instance containing the event data.</param>
  51. Private Sub backWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles backWorker.RunWorkerCompleted
  52. statusLabel.Text = "Done!"
  53. MessageBox.Show(localIP, "IP Address", MessageBoxButtons.OK)
  54. btnGo.Enabled = True
  55. End Sub
  56.  
  57. ''' <summary>
  58. ''' Handles the Load event of the Project1 control.
  59. ''' Sets input focus to the prefilled combobox for user input of the hostname.
  60. ''' </summary>
  61. ''' <param name="sender">The source of the event.</param>
  62. ''' <param name="e">The <see cref="T:System.EventArgs" /> instance containing the event data.</param>
  63. Private Sub Project1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  64. Me.hostNameComboBox.SelectAll()
  65. Me.hostNameComboBox.Focus()
  66. End Sub
  67.  
  68. ''' <summary>
  69. ''' Handles the ProgressChanged event of the backWorker control.
  70. ''' Updates the progressBar based on percentage of job done.
  71. ''' </summary>
  72. ''' <param name="sender">The source of the event.</param>
  73. ''' <param name="e">The <see cref="T:System.ComponentModel.ProgressChangedEventArgs" /> instance containing the event data.</param>
  74. Private Sub backWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles backWorker.ProgressChanged
  75. progressBar.Value = e.ProgressPercentage
  76. End Sub
  77. 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...
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1
Reputation: mydotnetemail is an unknown quantity at this point 
Solved Threads: 0
mydotnetemail mydotnetemail is offline Offline
Newbie Poster

Re: Understanding the BackgroundWorker Process

 
0
  #2
May 13th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 1
Reputation: davidps is an unknown quantity at this point 
Solved Threads: 0
davidps davidps is offline Offline
Newbie Poster

Re: Understanding the BackgroundWorker Process

 
0
  #3
Mar 23rd, 2009
hi!
i've solved the problem: the only thing you have to add is:
  1. BackWorker.WorkerReportsProgress = True
that's it!

davidps
(i really don't know if someone need it now...)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the VB.NET Forum


Views: 25919 | Replies: 2
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC