Exemplo n.º 1
0
        /// <summary>
        /// Executes the synchronsation
        /// Blocking call till cancellation is requested
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="config"></param>
        static void ExecuteSynchronisation(ILogger logger, Configuration config)
        {
            SyncManager sync = new SyncManager(_logManager.GetLogger(LoggerType.Default));

            TimeSpan reoccurence = new TimeSpan(0, 0, config.ReoccurenceTime);

            logger.Debug("Sync will be performed every: " + reoccurence.ToString(@"dd\:hh\:mm\:ss") + " (dd:hh:mm:ss)");

            //Variables for Reoccurence and stop
            CancellationTokenSource _cancleToken    = new CancellationTokenSource();
            ManualResetEventSlim    _serverSideSync = new ManualResetEventSlim(true);

            //Timer for reoccurence
            System.Timers.Timer reoccurenceTimer = new System.Timers.Timer();
            reoccurenceTimer.Interval = config.ReoccurenceTime * 1000;
            reoccurenceTimer.Elapsed += (sender, args) =>
            {
                _serverSideSync.Set();
            };
            reoccurenceTimer.Enabled = true;

            //Event for cancleation
            Console.CancelKeyPress += (sender, args) =>
            {
                logger.Debug("Canceling");
                _cancleToken.Cancel();
                args.Cancel = true;
            };

            //Blocking operation!
            //As long as no cancellation is reqeusted, execute the Serverside checkout if reset event is set
            try
            {
                do
                {
                    int index = WaitHandle.WaitAny(new WaitHandle[] { _serverSideSync.WaitHandle, _cancleToken.Token.WaitHandle, });
                    if (index == 0)
                    {
                        //The ServerSideSync event was set
                        sync.ExcecuteSynchronisation(_cancleToken.Token, config, new WebDavSyncClient(logger, config));
                        //Reset later -> If the execution times overlap they will be
                        //skipped instead of increasing the hanging buffer
                        _serverSideSync.Reset();
                    }
                } while(!_cancleToken.IsCancellationRequested);
            } catch (Exception exc)
            {
                logger.Error("FATAL! An unexpected exception occured during execution:", exc);
            } finally
            {
                //Dispose everything
                _cancleToken.Dispose();
                _serverSideSync.Dispose();
                reoccurenceTimer.Dispose();
                logger.Debug("Application terminated");
                logger.Debug("");
            }
        }