Re: Problem using BackGroundWorker to ping multiple LAN hosts
- From: "Michael M." <nospam@xxxxxxxx>
- Date: Tue, 13 Mar 2007 20:42:09 -0000
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<snip>
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:
It is almost like the the Argument is passed "by ref" meaning by the time<snip>
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
The code that calls the Background workers (it is within a loop in the<snip>
actual program)
If bgwPinger.IsBusy = False Then
IpHostOctect += 1
bgwPinger.RunWorkerAsync(IpHostOctect)
End If
If bgwPinger2.IsBusy = False Then
IpHostOctect += 1
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.
.
- References:
- Problem using BackGroundWorker to ping multiple LAN hosts
- From: Michael M.
- Re: Problem using BackGroundWorker to ping multiple LAN hosts
- From: Branco Medeiros
- Problem using BackGroundWorker to ping multiple LAN hosts
- Prev by Date: Re: system call
- Next by Date: Re: vb.net search network drive
- Previous by thread: Re: Problem using BackGroundWorker to ping multiple LAN hosts
- Next by thread: Passing exceptions from a Dll back to the calling application.
- Index(es):
Relevant Pages
|