Handling slow operations in OnIdle

I am trying to handle some slow file processing in OnIdle (reading and parsing XML file), but am running in to problems because the operations take too long. The way I understand it, the OnIdle call is driven by a Timer, however every time the Timer runs out the OnIdle call is restarted from the beginning and any running tasks are aborted. That is, OnTimer seems to cause a jump to the start of the OnIdle call regardless of the current state.

Is there some way to alter this behavior, for instance continuing code execution where OnIdle left off before jumping back to the start of OnIdle? Or maybe there’s a better way of handling these long operations which I’m not seeing? I guess a new thread could be created to handle such an operation, but then things get complicated when the UI gets involved (e.g. updating controls based on file data), which is why I chose OnIdle in the first place.

It’s very possible my implementation is suboptimal, however it seems dangerous that OnIdle would work like this without throwing any errors. As it is, you basically have to guess if your operations are short enough to be executed within the refresh period. Maybe some flag could be set at the start of OnIdle and cleared upon finishing the call. If the flag is still set, an error is thrown.

OnIdle() seems a very weird place to do that. You probably want to use a thread that gets launched after some UI event, or flag getting set.