iBarShift, iBars gaps in history when computer awakes from sleep mode

 
I am pulling out what little hair I have because of this problem.

Here is what is happening.

I have created an "indicator" program that exports data from a chart on a tick by tick basis to a DLL that then passes it on to another program for plotting.

Within the program each time I write out a bar I save the time of the last bar.

I also allow the user to define multiple time frames to export from a single chart. This way, the user only has to setup one chart for a currency pair to export data for up to 8 time frames.

This is where I use the iBarShift and iBars functions.

Each time a tick comes in I use the last bar time that is saved for each time frame and use the iBarShift function to get the bar offset for that last bar and then output the OHLCV information to the DLL. The reason I use iBarShift is that if there are missing bars between the last bar time I exported and the current bar I would be able to get those bars and export them to fill in the gaps.

There are two problems:

1. If I try to export a time frame that is not the time frame of the current chart then sometimes it does not export anything of very little. Through research I have found that I must look for the 4066 error code right after the call to iBarShift. If I get a 4066 error code then I should pause and retry again. So far so good. I think this problem might be solved using this process.

2. Many times if a user leaves there computer unattended the computer goes to sleep. This is especially true for notebook computers. So the computer goes to sleep for say a few hours or even less. When the computer awakes it starts up and the latest tick is received by the chart. This is where the problem occurs. The iBarShift always returns a 1 for the offset of the last Bar time. It is missing all the bars between the current bar and the last bar that I exported just before the computer went to sleep. Let me give an example to help clarify this.

Lets say I have a 1 min chart, and I say I want to export 5 min bars. Now lets say the computer goes to "sleep" for about an hour. So during that hour about 12 more 5 min bars should have formed. Obviously when the computer is a sleep the chart will not be updating. However, when the computer awakes the current 1 min chart updates it bars and fills in the missing bars that occurred when the computer was a a sleep.
However, when I do the iBarShift function to find that offset of the last bar for the 5 min time frame that I output previously I get an offset of 1. This is incorrect because the offset should be 12. It is basically missing all the bars when the computer went to sleep. I thought that maybe this could be solved by checking the 4066 error like in problem #1 but this did not work because the error code returned was 0... meaning no error. I even tried to pause the program for 2 secs and try the iBarShift again but it still brought back the offset as 1.
I also tried to change my program to use iBars instead of iBarShift but the same problem exists.... missing bars.

What's really weird is that even if I choose to use the iBarShift using the current symbol and current chart's timeframe i get the same missing bars problem.

Does anyone know what is happening here?

Does anyone know of a possible solution to this?

Thanks
Reason: