Ejemplo n.º 1
0
        private async Task RunAllPendingJobs()
        {
            // Monitor cannot be used here, since Monitor allows recursive enter for a thread
            if (Interlocked.CompareExchange(ref _isRunning, 1, 0) == 0)
            {
                try
                {
                    if (_profile.CheckIfOnline && !await ConnectionTester.IsOnline(_profile.ProxyOptionsOrNull))
                    {
                        s_logger.WarnFormat("Skipping synchronization profile '{0}' (Id: '{1}') because network is not available", _profile.ProfileName, _profileId);
                        return;
                    }

                    while (_fullSyncPending || _pendingOutlookItems.Count > 0)
                    {
                        if (_fullSyncPending)
                        {
                            _fullSyncPending = false;
                            Thread.MemoryBarrier(); // should not be required because there is just one thread entering multiple times
                            await RunAndRescheduleNoThrow();
                        }

                        if (_pendingOutlookItems.Count > 0)
                        {
                            var itemsToSync = _pendingOutlookItems.Values.ToArray();
                            _pendingOutlookItems.Clear();
                            if (s_logger.IsDebugEnabled)
                            {
                                s_logger.Debug($"Partial sync: Going to sync '{itemsToSync.Length}' pending items ( {string.Join(", ", itemsToSync.Select(id => id.EntryId))} ).");
                            }

                            Thread.MemoryBarrier(); // should not be required because there is just one thread entering multiple times
                            await RunPartialNoThrow(itemsToSync);
                        }
                    }
                }
                finally
                {
                    Interlocked.Exchange(ref _isRunning, 0);
                }
            }
        }
        private async Task RunAllPendingJobs()
        {
            if (_checkIfOnline && !ConnectionTester.IsOnline())
            {
                s_logger.WarnFormat("Skipping synchronization profile '{0}' (Id: '{1}') because network is not available", _profileName, _profileId);
                return;
            }

            // Monitor cannot be used here, since Monitor allows recursive enter for a thread
            if (Interlocked.CompareExchange(ref _isRunning, 1, 0) == 0)
            {
                try
                {
                    while (_fullSyncPending || _pendingOutlookItems.Count > 0)
                    {
                        if (_fullSyncPending)
                        {
                            _fullSyncPending = false;
                            Thread.MemoryBarrier(); // should not be required because there is just one thread entering multiple times
                            await RunAndRescheduleNoThrow();
                        }

                        if (_pendingOutlookItems.Count > 0)
                        {
                            var itemsToSync = _pendingOutlookItems.ToArray();
                            _pendingOutlookItems.Clear();
                            Thread.MemoryBarrier(); // should not be required because there is just one thread entering multiple times
                            await RunIfResponsibleNoThrow(itemsToSync);
                        }
                    }
                }
                finally
                {
                    Interlocked.Exchange(ref _isRunning, 0);
                }
            }
        }