Domanda

I'm using Microsoft Sync Framework and am Synching every 10 seconds over a WCF service. On some system I have run into weird SemaphoreFullExceptions, so I'm trying to Refactor this Synching Background Thread. Basically my question is: what approach to prefer:

private void SynchronizedWorker()
    {
        // Sync loop
        while (!_shouldStop)
        {
            using (var conn = new SqlConnection())
            {
                SyncOrchestrator orchestrator = new SyncOrchestrator();
                SqlSyncProviderProxy destinationProvider = new SqlSyncProviderProxy("TEST");
                SqlSyncProvider localProvider = new SqlSyncProvider("TEST", conn);
                // Set Providers
                orchestrator.LocalProvider = localProvider;
                orchestrator.RemoteProvider = destinationProvider;
                // Direction
                orchestrator.Direction = SyncDirectionOrder.UploadAndDownload;


                // Sync
                orchestrator.Synchronize();
            }


            // wait for next interval or being woken up
            if (_syncWaitEvent.WaitOne(10000))
            {
                // we have been signaled prior to the timeout expiring
                _syncForce = false;
            }
        }
    }

or this

SqlConnection conn;
    SyncOrchestrator orchestrator = new SyncOrchestrator();
    SqlSyncProviderProxy destinationProvider = new SqlSyncProviderProxy("TEST");
    SqlSyncProvider localProvider = new SqlSyncProvider("TEST", conn);

    private void SynchronizedWorker2()
    {
        conn = new SqlConnection();
        orchestrator = new SyncOrchestrator();
        destinationProvider = new SqlSyncProviderProxy("TEST");
        localProvider = new SqlSyncProvider("TEST", conn);
        // Set Providers
        orchestrator.LocalProvider = localProvider;
        orchestrator.RemoteProvider = destinationProvider;
        // Direction
        orchestrator.Direction = SyncDirectionOrder.UploadAndDownload;

        // Sync loop
        while (!_shouldStop)
        {
            // Sync
            orchestrator.Synchronize();

            // wait for next interval or being woken up
            if (_syncWaitEvent.WaitOne(10000))
            {
                // we have been signaled prior to the timeout expiring
                _syncForce = false;
            }
        }
    }

Creating new objects for each sync for me seems to be the cleaner way to go. But of course it costs slightly more cpu time.

What do you think?

È stato utile?

Soluzione

What I've done is use the same Orchestrator for sync, but In two cases I am disposing the objects (and recreating if needed):

  • An error occured
  • The application is being exited

Think this is the way to go...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top