locked
Asynchronous Syncing RRS feed

  • Question

  • Hi,

     

    I'm looking into whether or not the actual Syncing can be done asynchronously. I have a book (Apress Pro Sync Framework, Singh/Kanjilal) which shows the SyncCallbacks class, along with the callback methods (including OnProgressChanged which is ideal for monitoring the progress of long running syncs) but does not show how to implement it.

     

    Also, if I look for this class in Sync Services for Devices (compact framework) I cannot find it. My sync client is a smart device application running on Windows Mobile 6.

     

    How would I go about implementing an asynchronous Sync, and can I obtain callbacks to monitor the progress of the sync?

     

    Many thanks in advance.

    • Moved by Max Wang_1983 Thursday, April 21, 2011 10:29 PM forum consolidation (From:SyncFx - Microsoft Sync Framework Database Providers [ReadOnly])
    Tuesday, January 20, 2009 10:00 AM

Answers

  • I am not aware of out of the box asynch methods being part of the sync provider.  I ended up writing my own.  It was pretty easy.  Just create a class called SyncManager for example.  Instead of calling the synchronize method directly, run your calls through your SyncManager class.  This class will contain a background worker which dispatches the calls to Synchronize.  Problem solved, now you synchronization occurrs on another thread.  Just make sure to invoke your calls to your UI when handling a synch event.

     

    As for the progress events, there are tons of examples of using them on this website. http://www.syncguru.com/Default.aspx

     

    Wednesday, January 21, 2009 8:21 PM
  • I think he was referring to the second post (above). You need to create a separate class which acts as a 'Sync Manager' to perform the sync. Within that class, have a method which spawns a new background thread. The new thread makes the call to SyncAgent->Synchronize.

    e.g.

            
    /// <summary>
    /// Update cache asynchronously
    /// </summary>
    public void UpdateCacheAsync()
    {
    if (IsSyncing == false)
    {
    IsSyncing = true; // reset the IsSyncing flag to indicate sync is in progress (don't want multi-instance of sync)

    thr = new Thread(new ThreadStart(this.Sync));
    thr.IsBackground = true;
    thr.Start();
    }
    }

    /// <summary>
    /// Sync the data
    /// </summary>
    private void Sync()
    {
    SyncAgent syncAgent = this.GetSyncAgent();
    try
    {
    SyncStatistics stats = syncAgent.Synchronize();
    this.HandleSyncCompleted(stats);
    }
    catch (Exception ex)
    {
    CAppLogger.LogException(ex);
    this.HandleSyncError(ex);
    }
    }



    • Marked as answer by P.Mc Tuesday, August 4, 2009 2:32 PM
    Saturday, August 1, 2009 7:40 AM

All replies

  • I am not aware of out of the box asynch methods being part of the sync provider.  I ended up writing my own.  It was pretty easy.  Just create a class called SyncManager for example.  Instead of calling the synchronize method directly, run your calls through your SyncManager class.  This class will contain a background worker which dispatches the calls to Synchronize.  Problem solved, now you synchronization occurrs on another thread.  Just make sure to invoke your calls to your UI when handling a synch event.

     

    As for the progress events, there are tons of examples of using them on this website. http://www.syncguru.com/Default.aspx

     

    Wednesday, January 21, 2009 8:21 PM
  • right. the out of box sync service doesn support async sync. but you can do this in the application level as described in this thread.

     

    thanks

    Yunwen

    Sunday, January 25, 2009 8:32 AM
    Moderator
  • Hi,

    I'm bumping this threading as I'm now at the stage in my project where I need to implement this.

    OverloadedOverrides, thanks for the link - I followed it but couldn't find the information you mentioned though.

    Yunwen, thanks for clarifying that it isn't supported out-of-the-box - you mentioned doing it at the application level "as described in this thread" but there's no thread link.

    Any more help you can give would be gratefully appreciated.

    Many thanks,

    Paul.
    Friday, July 31, 2009 4:30 PM
  • I think he was referring to the second post (above). You need to create a separate class which acts as a 'Sync Manager' to perform the sync. Within that class, have a method which spawns a new background thread. The new thread makes the call to SyncAgent->Synchronize.

    e.g.

            
    /// <summary>
    /// Update cache asynchronously
    /// </summary>
    public void UpdateCacheAsync()
    {
    if (IsSyncing == false)
    {
    IsSyncing = true; // reset the IsSyncing flag to indicate sync is in progress (don't want multi-instance of sync)

    thr = new Thread(new ThreadStart(this.Sync));
    thr.IsBackground = true;
    thr.Start();
    }
    }

    /// <summary>
    /// Sync the data
    /// </summary>
    private void Sync()
    {
    SyncAgent syncAgent = this.GetSyncAgent();
    try
    {
    SyncStatistics stats = syncAgent.Synchronize();
    this.HandleSyncCompleted(stats);
    }
    catch (Exception ex)
    {
    CAppLogger.LogException(ex);
    this.HandleSyncError(ex);
    }
    }



    • Marked as answer by P.Mc Tuesday, August 4, 2009 2:32 PM
    Saturday, August 1, 2009 7:40 AM
  • Thanks SunHunter - I've got it working now!

    Much Appreciated!
    Tuesday, August 4, 2009 2:32 PM
  • Sunhunter, could you post your SyncManager class ?  I read about it in this thread (http://social.microsoft.com/Forums/en-US/uklaunch2007ado.net/thread/aba12aa1-6183-419a-af7c-0446e004f6cb/), but apparently am missing something.

    In my code, I init a SyncAgent, and specify some LocalProvider events and properties. Then I pass the syncagent to the SyncManager.

    I get a CientConnection is null error. If I sync without the syncManager, it is fine.
    Monday, October 5, 2009 2:30 PM
  • Thanks SunHunter - I've got it working now!

    Much Appreciated!
    Paul - Care to share an example ?
    Tuesday, December 1, 2009 8:48 PM