473,765 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem using BackGroundWorke r to ping multiple LAN hosts

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:

192.168.0.1 online

192.168.0.2 offline

192.168.0.3 online

192.168.0.4 offline

It seems to return this (like the IPHostOctet is not incrementing before I
fire of the worker processes)

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

FYI: inside the do work methods of the Background workers it just appends
the incrmeting network host byte / octect like:

reply = Pinger.Ping (192.168.0. & IpHostOctect.to string)

192.168.0.1 offline

192.168.0.1 offline

192.168.0.1 offline

192.168.0.1 offline

192.168.0.2 offline

192.168.0.2 offline

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

If bgwPinger.IsBus y = False Then

IpHostOctect += 1

bgwPinger.RunWo rkerAsync(IpHos tOctect)

End If

If bgwPinger2.IsBu sy = False Then

IpHostOctect += 1

bgwPinger2.RunW orkerAsync(IpHo stOctect)

End If

If bgwPinger3.IsBu sy = False Then

IpHostOctect += 1

bgwPinger3.RunW orkerAsync(IpHo stOctect)

End If

If bgwPinger4.IsBu sy = False Then

IpHostOctect += 1

bgwPinger4.RunW orkerAsync(IpHo stOctect)

End If

Mar 9 '07 #1
5 5559
Michael M. 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:
I was curious about your problem so I copied your code exactly and found
that unless I inserted -

System.Windows. Forms.Applicati on.DoEvents()

after each call the the BackGround Worker, the BGW would only run once
and then my application would appear to hang.

Once the DoEvents was inserted, everything worked perfectly.

By the way, in each of my BGW Subs I used the following code (watch for
wrapping) -

Dim Ping As New Ping
Dim PingReply As PingReply = Ping.Send("192. 168.1." & e.Argument.ToSt ring)
Debug.Print(Str ing.Format("192 .168.1.{0} {1}", e.Argument.ToSt ring,
IIf(PingReply.S tatus = IPStatus.Succes s, "Online", "Offline")) )
Hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Mar 9 '07 #2
ShaneO wrote:
Michael M. 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:

I was curious about your problem so I copied your code exactly and found
that unless I inserted -

Dim PingReply As PingReply = Ping.Send("192. 168.1." & e.Argument.ToSt ring)
Debug.Print(Str ing.Format("192 .168.1.{0} {1}", e.Argument.ToSt ring,
IIf(PingReply.S tatus = IPStatus.Succes s, "Online", "Offline")) )
By the way, I found if I used the "TimeOut" value in my Ping call, as in
the following -

Dim PingReply As PingReply = Ping.Send("192. 168.1." &
e.Argument.ToSt ring, 200)

the code would run very quickly as the default TimeOut value obviously
allows too much time. Setting this value too low (<100) did not always
allow enough time for each computer to respond, so I would obtain false
"Offline" results.
ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
Mar 9 '07 #3
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.IsBus y = False Then

IpHostOctect += 1

bgwPinger.RunWo rkerAsync(IpHos tOctect)

End If

If bgwPinger2.IsBu sy = 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.DoWo rk, _
bgwPinger2.DoWo rk, _
bgwPinger3.DoWo rk, _
bgwPinger4.DoWo rk

'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.ToStr ing)

E.Result = Reply
End Sub

Private Sub WorkersDone( _
Sender As Object, _
E As RunWorkerComple tedEventArgs _
) Handles bgwPinger1.RunW orkerCompleted, _
bgwPinger2.RunW orkerCompleted, _
bgwPinger3.RunW orkerCompleted, _
bgwPinger4.RunW orkerCompleted

'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 BackgroundWorke r = _
CType(Sender, BackgroundWorke r)
PingNext(ThisWo rker)

End Sub

Private Sub PingNext(Worker As BackgroundWorke r)
'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.RunWorke rAsync(mHostOct et)
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(bgwPin ger1)
PingNext(bgwPin ger2)
PingNext(bgwPin ger3)
PingNext(bgwPin ger4)
'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.

Mar 12 '07 #4
Thanks ShaneO,

You are right the code does work. It worked correctly when I pasted it in
to a new project.

This was such a strange problem, It had something to do with the
IDE/Complier caching something, I was changing my code but the changes would
never be reflected when compiled / run until I deleted the contents of the
obj folder that gets created every time you compile a project.

It got as bad as the fact it was running a BGW do work SUB that no longer
exsited in the project.

As far as I know the Application.Doe vents should be called within any long
loop that is executed on the GUI thread to keep it responsive.

Regards,

Mike

"ShaneO" <sp****@optusne t.com.auwrote in message
news:45******** **************@ news.optusnet.c om.au...
Michael M. 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:

I was curious about your problem so I copied your code exactly and found
that unless I inserted -

System.Windows. Forms.Applicati on.DoEvents()

after each call the the BackGround Worker, the BGW would only run once and
then my application would appear to hang.

Once the DoEvents was inserted, everything worked perfectly.

By the way, in each of my BGW Subs I used the following code (watch for
wrapping) -

Dim Ping As New Ping
Dim PingReply As PingReply = Ping.Send("192. 168.1." & e.Argument.ToSt ring)
Debug.Print(Str ing.Format("192 .168.1.{0} {1}", e.Argument.ToSt ring,
IIf(PingReply.S tatus = IPStatus.Succes s, "Online", "Offline")) )
Hope this helps.

ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.

Mar 13 '07 #5
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" <br************ *@gmail.comwrot e in message
news:11******** **************@ 64g2000cwx.goog legroups.com...
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.IsBus y = False Then

IpHostOctect += 1

bgwPinger.RunWo rkerAsync(IpHos tOctect)

End If

If bgwPinger2.IsBu sy = 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.DoWo rk, _
bgwPinger2.DoWo rk, _
bgwPinger3.DoWo rk, _
bgwPinger4.DoWo rk

'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.ToStr ing)

E.Result = Reply
End Sub

Private Sub WorkersDone( _
Sender As Object, _
E As RunWorkerComple tedEventArgs _
) Handles bgwPinger1.RunW orkerCompleted, _
bgwPinger2.RunW orkerCompleted, _
bgwPinger3.RunW orkerCompleted, _
bgwPinger4.RunW orkerCompleted

'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 BackgroundWorke r = _
CType(Sender, BackgroundWorke r)
PingNext(ThisWo rker)

End Sub

Private Sub PingNext(Worker As BackgroundWorke r)
'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.RunWorke rAsync(mHostOct et)
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(bgwPin ger1)
PingNext(bgwPin ger2)
PingNext(bgwPin ger3)
PingNext(bgwPin ger4)
'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.

Mar 13 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4373
by: DCK | last post by:
Hello :) Into group-archive i found most e-mails, which touches PINGing. In my work i've used TELNET for testing if host is operational. Sometimes, for unknown reasons, workstation doesn't respond for PINGing. But in WinNT network, all hosts has a nbsession listening on port 139. I always use this script instead PING command (hope, will be usefull for someone :) ):
19
1818
by: Kevin | last post by:
I have been using java (jsp/servlets), vb/asp and perl for a few years. Almost all my projects are web site development related, and many of my clients' web sites are hosted on those shared web hosting services. The problem is that it's difficult to find hosting services with Python installed and supported. And most hosting companies are reluctant to install it for you because they don't want to do the extra work(they would always say to...
3
1437
by: Tim Gallivan | last post by:
Has anyone stumbled upon any FREE code for ping hosts from vb.net? Any links would be greatly appreciated. Thanks in advance, -- Tim Gallivan I know I'm a great teacher because when I give a lesson, the person never comes back.
11
10329
by: Mikael Arhelger | last post by:
Hello, This has been posted a few times but still I could not find a way to connect to our database. We run DB2 Express on WIN2K server with XP clients. I can ping inside network and to the Internet from the server, so TCP/IP is ok. The DB2COMM=TCPIP is set also. Have been scratching my head for days now. Here some additional data (we are running SAP Business One):
9
5023
by: =?Utf-8?B?VE9NX1Bhc2FkZW5h?= | last post by:
Hello, In my ASP.Net app I'm launching a BackgroundWorker thread in my Page_Load function. In that thread I'm attempting to connect to a SQL server using this connection string "Initial Catalog=MyDB;Data Source=MyServer;Integrated Security=True;". When I use the same connection string in my main thread of execution it works fine. However when I use that connection string in the newly created thread I get this exception: "Login...
0
2480
by: Chris | last post by:
I would like to be able to pass the BackgroundWorker object and DoWork Event Args to a second function (third function?) and be able to still report the progress. I'm getting the following exception when trying to access BackgroundWorker.CancellationPending or e.Cancel An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll Additional information: Exception has been thrown by the target of...
3
8372
by: imughal | last post by:
I got the perl script which does following task. This solution reads in a line from a text file that is passed in as input parameter 1 to a Perl script. This script will then ping the machine using TCP pings to the remote hosts echo port and will return up/down and the ping response time. A second optional attribute may be passed in that will set the default timeout for the ping response. Text file format: 127.0.0.0 localhost...
2
1646
by: Jeff | last post by:
Hey ..NET 2.0 I'm developing an application which will perform some webservice calls and I believe having those calls in a separate thread may help the app run smoother No user are waiting for the result of these webservice calls, Each night this code calls some webservices, which return a result I need to store in
1
4800
by: schnandr | last post by:
Hi, I have a user control which contains ListView. I copy all ListViewItems into a separate List<ListViewItemcalled unfilteredItems. I am using a BackgroundWorker to filter the ListView. When I access this list in the DoWork Handler I am getting an InvalidOperationException that the List was not created by this thread. Is there a way to solve this problem?
0
9398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9951
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7378
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.