I have found what looked like a perfect way to get a hold on this:
Protected Overrides Sub OnShutdown()
If oRobot.lBusy Then
oRobot.lShutDown = True
RequestAdditionalTime(30000)
Else
If Not oRobot.StopRobot() Then
End If
End If
End Sub
Basically, I added a global property in the class lBusy. When the robot loop is in progress, this is True. If this is True, additional requested time will be applied.
So far so good, I can see it goes in there, it sees it is busy, it requests additional time, it goes back in here a second time, and this will go until the robot is no longer busy.
The problem here is I am loosing resources. The robot needs to have access to drives and things like that. As the shutdown event is proceeding with whatever it can, it starts by stopping other services, my robot is still running, but then it cannot write a file, communicate with SQL Server and so on.
So, this thinking was good but it would only work if I can put a hold on all other shutdown operations until this one is clean. And, I do not see how that could be possible. So, as resources are needed, I guess we will have to remain with those procedures. Someone has to initiate a stop of all the robots from the Web interface. That will stop them all. As no shutdown is effective, all resources needed as still available. So, if a robot is not busy, it closes, otherwise, it will close after its process. Then, someone can initiate the shutdown request.
Unless you see a way to apply a lock right at the top of the shutdown chain, I really do not see how this could be possible.