每5秒与服务器同步一次

时间:2013-09-11 07:17:21

标签: c# web-services polling microsoft-sync-framework

我有一个服务器 - 客户端环境,客户端每隔5秒就会连接并与服务器同步数据。

对于同步我在客户端上使用后台线程工作正常,但在一个特定的硬件上GetNetworkAvailable方法偶尔会抛出内存异常。错误发生在逻辑上,所以我猜这些硬件并不好。无论如何,我想对我的背景 - 民意调查功能有所了解。

private void SynchronizedWorker()
    {
        bool initialization = true;
        int initializationTries = 0;

        // Sync loop
        while (!_shouldStop)
        {
            // Initialize locals
            int syncInterval = _syncInterval; // Reset Sync Interval to default
            SyncStatus syncStatus = SyncStatus; // Get current Status
            bool hasNetworkConnection = true; // Has Network connection

            try
            {
                //*******************************************
                // Check Network connection
                //*******************************************
                if (!NetworkInterface.GetIsNetworkAvailable())
                {
                    hasNetworkConnection = false;
                    _syncResetEvent.Set();
                    syncStatus = SyncStatus.Offline;
                }
                else if (syncStatus == SyncStatus.Offline) // Is in offline - State, but Network available again recognize going online
                {
                    _syncResetEvent.Set();
                    hasNetworkConnection = IsServiceAvailable();
                    // Sync-Status stays offline
                    if (!hasNetworkConnection)
                    {
                        // Wait 1 minute, before retry
                        syncInterval = 60000;
                    }
                }

                //*******************************************
                // Proceed with Syncronisation
                //*******************************************
                if (hasNetworkConnection)
                {
                    syncStatus = SyncStatus.Ok;
                    // Try Initialization
                    if (initialization)
                    {
                        try
                        {
                            // Sync initialization here...
                            // ..............................
                            ////////////////////////////////////
                        }
                        catch (Exception ex)
                        {
                            log.LogError(string.Format("Initialization Try {0}", initializationTries), ex);
                            syncStatus = SyncStatus.InitializationFailed;
                        }
                    }

                    // To many Init-Tries --> Stop
                    if (initializationTries > 1)
                    {
                        // Proceed
                        _syncResetEvent.Set();
                        _syncStatus = syncStatus;
                        break; // Stop Synch
                    }

                    // Debug
                    var sw = System.Diagnostics.Stopwatch.StartNew();

                    // Do Up/Download
                    if (!initialization)
                    {
                        try
                        {
                            // Synchronisation with service here...
                            // ..............................
                            ////////////////////////////////////

                            // Proceed
                            _syncResetEvent.Set();
                        }
                        catch (Exception ex)
                        {
                            log.LogError("Download", ex);
                            syncStatus = SyncStatus.SynchFailed;
                        }
                    }
                }

                // Set Sync status
                _syncStatus = syncStatus;
            }
            catch (Exception ex)
            {
                log.LogError("Sync-Worker", ex);
                _syncStatus = Core.Sync.SyncStatus.SynchFailed;
                // Proceed
                _syncResetEvent.Set();
                // Reset Force
                _syncForce = false;
            }
            finally
            {
                // Set Completed
                if (OnSyncCompleted != null)
                {
                    OnSyncCompleted(downloaded, uploaded, executed);
                }

                // Always Wait for Interval
                DateTime sleepuntil = DateTime.Now.AddMilliseconds(syncInterval);
                while (sleepuntil > DateTime.Now && !_shouldStop && !_syncForce)
                {
                    // Sleep
                    Thread.Sleep(100);
                }
            }
        }

        // Stop
        _syncStatus = SyncStatus.Unknown;
 }

0 个答案:

没有答案