I have a task that queries the local database and posts it to a web api. At the moment it takes all the rows, breaks them into chunks of 500 and then posts them all simultaneously using an async thread setup. I have another job that occasionally gets data from the server and I was thinking that if I returned the server load and stored it in Config.ServerLoad
, it could space out the requests a bit if the server is getting hammered. Here is the code I use to setup my threads:
var json = JsonConvert.SerializeObject(rowDto);
var threads = new Thread(new ParameterizedThreadStart(MultiThreadPostThead));
var thisThread = new PostParams() { postUrl = postUrl, json = json, callingThread = threads };
threads.Start(thisThread);
threads.IsBackground = true;
ThreadHandles.Add(thisThread);
I was hoping to add something like this:
thread.delay(Config.ServerLoad * 1000);
If for instance the server load was 0.5, there would be almost no delay between threads, but if it was 10, it would wait 10 seconds between posts. I saw some info about a Task.Delay()
, but didn't see anything for threads. Is there something I missed that would help add a dynamic value delay or set a max number of simultaneous threads?