views:

20

answers:

0

I have a client WinForm that uses a web service to write to a server database. When it loads, it starts a thread that polls the server for new entries to the database. When found, the new entries are downloaded and stored in a cache (embedded database). The thread flow is as follows:

while(continueSynchronizing)
{
  foreach(Table t in database)
  {
    // This is done in a new thread
    Datetime lastEntry = t.LastEntryTimestamp;
    Entries newEntries = Server.GetEntryesForTableAfterAsOfTimestamp(t, lastEntry);
    foreach(Entry newEntry in newEntries)
    {
      if(UserHasForcedUpdate)
        break;
      StoreInCache(newEntry);
    }
  }

  // When all threads are complete
  FireEventStatingSynchronizationIsComplete

  WaitForFiveMinutesOrUntilUserForcesUpdate()
}

In general this has worked out well for me. However, I would like to change the event. When entering new data, the client would have to wait for the synchronization thread before seeing the new data. Currently I am attaching the event handler, writing to the server, and acting on the event handler when it fires. This can be quite inaccurate though. The synchronization may be in the middle of the loop when I attach to the event and then the event fires not for my desired loop, but the previous. What would be the best way to write the event such that I know its the loop I want?

Would it be as simple as adding a Start and Finish time stamp for the event args and comparing? If so that means every event handler that cared which loop was fired would need to keep track of the current loop and then compare the time stamps for the loop they desire. Is there a way I can say attach to the next full loop's event handler after a starting time?