Re: Problem using BackGroundWorker to ping multiple LAN hosts

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Branco,

I was just trying to keep things simple in the explanation.

The "loop" is realy a timer object, but yes it's not the best option (I was
never realy happy with the timer loop executing and not having any thing to
do (when all the threads are busy)).

I will consider your code / way of using background workers (I have never
used them before).

Thanks for taking the time to write the example code.

I should have explained this better.

Mike.
"Branco Medeiros" <branco.medeiros@xxxxxxxxx> wrote in message
news:1173666469.734643.136940@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Michael wrote:
I have the following code (listed at bottom of post) that pings a small
range of IP address to see which ones are alive.

To speed things up a little I am trying to use more than one thread,
problem
is instead of returning:
<snip>

It is almost like the the Argument is passed "by ref" meaning by the time
the first thread fires it has been incremened 4 times and holds that
value
the same value in each bgworker / ping object.

or mabye it only gets incremened once per loop? It is declred outside
the
procedure and is of type Integer
<snip>

The code that calls the Background workers (it is within a loop in the
actual program)

If bgwPinger.IsBusy = False Then

IpHostOctect += 1

bgwPinger.RunWorkerAsync(IpHostOctect)

End If

If bgwPinger2.IsBusy = False Then

IpHostOctect += 1
<snip>

This is not the way to do it, as you may have guessed. Calling the
BGWorkers in a loop that waits 'til they're not busy is a tremendous
waste of resources and will simply freeze your UI, as ShaneO has
showed you. You'd get better by simply calling Ping.Send() in the
loop, instead...

I suggest you ditch the loop and focus on synchronizing the calls to
the BGWorkers. It's way more work, yes, but will give you the kind of
response you expect (I guess). Something in the lines of:

<aircode>
'At form level, the current octet:
Private mHostOctet As Byte

Private Sub WorkersWork( _
Sender As Object, _
E As DoWorkEventArgs _
) Handles bgwPinger1.DoWork, _
bgwPinger2.DoWork, _
bgwPinger3.DoWork, _
bgwPinger4.DoWork

'This issues a ping in another thread.

Dim Pinger As New Ping
Dim HostOctet As Byte = CInt(E.Argument) And 255
Dim Reply As PingReply = Pinger.Send( _
STR_BASEADDR & HostOctet.ToString)

E.Result = Reply
End Sub

Private Sub WorkersDone( _
Sender As Object, _
E As RunWorkerCompletedEventArgs _
) Handles bgwPinger1.RunWorkerCompleted, _
bgwPinger2.RunWorkerCompleted, _
bgwPinger3.RunWorkerCompleted, _
bgwPinger4.RunWorkerCompleted

'This is called in the main thread
'when each worker finishes

Dim Reply As PingReply = CType(E.Result, PingReply)

'Do Whatever you want with the reply

'Continues pinging with this bgworker
Dim ThisWorker As BackgroundWorker = _
CType(Sender, BackgroundWorker)
PingNext(ThisWorker)

End Sub

Private Sub PingNext(Worker As BackgroundWorker)
'Calls the aupplied worker with the current value
'of the host octet and increments the octet, so the
'next worker pings another address

If mHostOctet < 255 then
Worker.RunWorkerAsync(mHostOctet)
mHostOctet += 1
End If
End Sub
</aircode>

And in the method that you used to activate the "pingers", you could
do something like this:

<aircode>
'Start the first octet:
mHostOctet = 1
PingNext(bgwPinger1)
PingNext(bgwPinger2)
PingNext(bgwPinger3)
PingNext(bgwPinger4)
'that's it.
</aircode>

Everytime a worker finishes, the WorkersDone method will execute (in
the main thread) where you can use the reply passed in the E.Result to
update the UI.

One more thing: If you go multithread (and I think you will) remember
that the Ping class already has a method that will ping in a separate
thread, so you don't really need the BGWorkers...

HTH.

Regards,

Branco.



.



Relevant Pages

  • Re: Problem using BackGroundWorker to ping multiple LAN hosts
    ... Private Sub WorkersWork(_ ... 'This issues a ping in another thread. ... Dim Pinger As New Ping ... 'Calls the aupplied worker with the current value ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Looping through Query to create multiple sheets in excel- Just need the loop
    ... I always get to this point and I can't figure out how to loop through ... I have put the string in the query at the bottom. ... Optional strWorkSheet As String, Optional strRange As ... Dim objXLSheet As Object 'Excel.Worksheet ...
    (microsoft.public.access.forms)
  • Re: vbs to run thru list of files
    ... the plan for a script should contain the spots where to insert ... 04 Dim objFSO, objFl ... Dim oFile, sSIZ, ... ... What about variables used in a loop: ...
    (microsoft.public.scripting.vbscript)
  • Re: vbs to run thru list of files
    ... 04 Dim objFSO, objFl ... Dim oFile, sSIZ, ... ... What about variables used in a loop: ... b Index außerhalb des gültigen Bereichs ...
    (microsoft.public.scripting.vbscript)
  • RE: Beginner VBA help with multiple tables
    ... Dim rsEmployees As DAO.Recordset ... add a record to the Hours recordset for each employee. ... conditional statements to further control your code within the loop, ... Keep in mind also that there are queries that can do this as well, ...
    (microsoft.public.access.modulesdaovba)