So here is the situation. I need to call the website that will start the search. This search continues for an unknown time, and the only way to find out if the search has completed is to periodically request the website to see if there is a “Download data” link somewhere on it (it uses some strange ajax call to javascript timer to check backend and refresh page, I think).
So here is the trick. I have hundreds of items that I need to search, one at a time. So I have code that looks something like this:
var items = getItems(); Parallel.ForEach(items, item => { startSearch(item); var finished = isSearchFinished(item); while(finished == false) { finished = isSearchFinished(item);
Now, obviously, this is not real code, because there may be things that cause isSearchFinished to always be false.
The obvious danger of an infinite loop aside, how would I properly save isSearchFinished () from the call again and again, but instead call it every, say, 30 seconds or 1 minute?
I know that Thread.Sleep () is not the right solution, and I think that the solution can be done using Threading.Timer (), but I am not very familiar with it, and there are so many thread options that I just not sure what to use.
source share