I have an app that I query a database for IP information, then Ping the IP's and log the results to a database. This takes a long time (testing with 200 takes about 5 minutes as they are all sites across public internet) due to waiting for response from
1 ping before sending the next ping for the next site.
I'd love some advice on how to run all of these at the same time, or in groups of say 500? Would using different threads be best for this? If so, can you give me an example? My code is as follows:
While (rdr.Read)
pingInfo.strLocation = rdr("txtLocation").ToString
pingInfo.strHost = rdr("txtHost").ToString
pingInfo.strIP = rdr("txtIPAddress").ToString()
sendPing(pingInfo) 'Calls my sub routine to ping the host, the part I want to call multiple times
End While
bluelinenetw...
Member
139 Points
218 Posts
Run the same task 5000 times simultaniously
Sep 28, 2011 08:32 PM|LINK
Hello all,
I have an app that I query a database for IP information, then Ping the IP's and log the results to a database. This takes a long time (testing with 200 takes about 5 minutes as they are all sites across public internet) due to waiting for response from 1 ping before sending the next ping for the next site.
I'd love some advice on how to run all of these at the same time, or in groups of say 500? Would using different threads be best for this? If so, can you give me an example? My code is as follows:
While (rdr.Read) pingInfo.strLocation = rdr("txtLocation").ToString pingInfo.strHost = rdr("txtHost").ToString pingInfo.strIP = rdr("txtIPAddress").ToString() sendPing(pingInfo) 'Calls my sub routine to ping the host, the part I want to call multiple times End WhileWeb Based Self Service Password Reset for Active Directory Accounts (OpenSource Project)
Careed
All-Star
18774 Points
3637 Posts
Re: Run the same task 5000 times simultaniously
Sep 28, 2011 08:39 PM|LINK
Look at the Parallel class and either For or ForEach method:
http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel_methods.aspx
"The oxen are slow, but the earth is patient."
bluelinenetw...
Member
139 Points
218 Posts
Re: Run the same task 5000 times simultaniously
Sep 28, 2011 08:43 PM|LINK
Yeah I saw that, can you give me an example of using for or foreach with an sql datareader?
I'm accustomed to using while reader.read, never used for or for each with it.
Web Based Self Service Password Reset for Active Directory Accounts (OpenSource Project)
UstesG
Contributor
2128 Points
459 Posts
Re: Run the same task 5000 times simultaniously
Sep 28, 2011 10:26 PM|LINK
But don't expect me to do your job!
bluelinenetw...
Member
139 Points
218 Posts
Re: Run the same task 5000 times simultaniously
Sep 30, 2011 08:28 PM|LINK
Thanks! Down to 23 seconds
System.Threading.Tasks.Parallel.ForEach(dt.AsEnumerable, Sub(drow As DataRow) pingInfo.strLocation = drow("txtLocation").ToString pingInfo.strHost = drow("txtHost").ToString pingInfo.strIP = drow("txtIPAddress").ToString sendPing(pingInfo) End Sub)Web Based Self Service Password Reset for Active Directory Accounts (OpenSource Project)
rossmc1
Member
644 Points
152 Posts
Re: Run the same task 5000 times simultaniously
Oct 01, 2011 10:17 PM|LINK
I was going to sugest ThreadPools , but i guess the above solution is better, and it will help me too later
Thanks for the info.