Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Oct 01, 2011 10:17 PM by rossmc1
Sep 28, 2011 08:32 PM|LINK
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:
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
Sep 28, 2011 08:39 PM|LINK
Look at the Parallel class and either For or ForEach method:
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.
Sep 28, 2011 10:26 PM|LINK
try something like this.
Dim sa As New Data.SqlClient.SqlDataAdapter
Dim dt As datatable
For each dr as datarow in dt.rows
'do something with data
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
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.