I have a list of data sets that need to be updated real time. I would like to process 10 out of 100+ at a time and then once one is done grab the next oldest line. Basically keep this loop going for an infinite amount of time. I am quite new to the world of Threading and been poking around at AsyncTask. Is there an example of this anyone can point me to? I have googled quite a bit but can’t find exactly what i’m looking for.
Share
AsyncTaskis better suited to one-off operations. For an ongoing task you can consider a worker thread.Disclaimer: I don’t claim this is the best way to do it, but it should give you some ideas and stuff to read up on.
Testing it I use a Unit test to kick it off. You can extend the unit test into a proper test to ensure that actions are being performed as expected. In my case they’re a good initial assertion to spike out behaviour.
Relevant output from the test:
Here you can see the worker going to sleep, then waking up to process items in the queue. Technically you can use a list, then fetch 10 items from the list before releasing it from the lock and process those 10 items before checking the list again.
When the class is disposed it releases the loop then waits a moment for the worker thread to terminate before aborting. Here you’d probably want to check for any outstanding items and either log that they will not be processed, or persist them to file for later processing.
Edit: I found the issue with the double-event… A better implementation would be to use a
ManualReseton theEventWaitHandleThen when handling the case where you process an item, resent the handle:
This produces the better test results: