I'm working on a program that will request information from our database, send that information to a webservice, then log to webservice response back to the database.
Here is the method that will call the webservice, it uses a few other custom classes but everything should be fairly self-explanatory.
public void doTransitTimeLoop(string botName)
{
while (keepProcessingTransitTime)
// loop until keep processing is set to false from the UI thread
{
// send the start query
DataTable dt = sendSqlQuery($"execute fex.transitStart @Bot='{botName}', @TransitCount={fexTransitInstaces}");
List<TransitTimePackage> packages = new List<TransitTimePackage>();
// TransitTimePackage is pretty much just a container class
string returnQuery = "";
// loop through rows on datatable
foreach (DataRow row in dt.Rows)
// create a package object from each row
{
TransitTimePackage package = TransitTimePackage.createFromDataRow(this, row);
packages.Add(package);
ThreadPool.QueueUserWorkItem((object stateInfo) => {
RestResponse res = package.sendWebserviceRequest();
package.parseResponse(res);
returnQuery += package.sqlQuery + "\n";
});
}
// check if all the packages are done, if they aren't, sleep 1 sec
while (packages.Any(p => p.StatusCode == HttpStatusCode.Created))
Thread.Sleep(1000);
DataTable d = sendSqlQuery(returnQuery);
}
}
I'm currently just pushing all the webservice calls into the threadpool so they can run concurrently, but that's only letting me make about 100 calls a minute because ThreadPool can only handle 4 threads at a time. Ideally I could send all my calls as quickly as possible, then wait for them all to finish and keep executing my code, but I'm not sure if that's possible (networking isn't my strongsuit aha).
Any help to get this running faster would be greatly appreciated. Thanks!