private void CheckTasks()
        {
            bool  lRefreshComboBox       = false;
            Int64 lSettingsChangeCounter = 0;

            for (int lIndex = 0; lIndex < fSettings.Settings.BackupTasks.Count; ++lIndex)
            {
                BackupTask lBackupTask       = fSettings.Settings.BackupTasks[lIndex];
                bool       lItemUpdateNeeded = false;

                // calculate global ChangeCount of all task, it will be used to see if there was some change made recently
                // we calculate ChangeCount only for parameters that we want to check
                lSettingsChangeCounter += lBackupTask.ChangeCounter + lBackupTask.Settings.ChangeCounter;

                // if paths are wrong we can't do anything, go to next task
                if (lBackupTask.Settings.SourcePathHelper.IsEmpty || String.IsNullOrEmpty(lBackupTask.Settings.DestinationPath))
                {
                    continue;
                }

                // check flag from SourceFileWatcher
                if (lBackupTask.SourceFilesChanged)
                {
                    // if auto backup enabled and check frequency
                    if (lBackupTask.Settings.IsAutoBackupActive && fBackupMaker.CheckBackupFrequency(lBackupTask))
                    {
                        string lErrorMessage  = "";
                        bool   lErrorOccurred = false;

                        bool lFileWasModified = false;
                        bool lFileIsLocked    = false;

                        // we will do something, anything happens it GUI need to be updated
                        lItemUpdateNeeded = true;

                        // check for files modification, function result success/error
                        if (!fBackupMaker.CheckFilesToBackup(lBackupTask, ref lFileWasModified, ref lFileIsLocked, ref lErrorMessage))
                        {
                            // error occurred (or file is locked)
                            lRefreshComboBox = true;
                            lErrorOccurred   = true;
                            lBackupTask.SetLastBackupStatus(!lErrorOccurred, lErrorMessage);
                        }
                        else if (lFileWasModified)
                        {
                            // success and file was modified
                            lErrorOccurred = !fBackupMaker.MakeBackup(lBackupTask, BackupType.btNormal, ref lErrorMessage);
                            lBackupTask.SetLastBackupStatus(!lErrorOccurred, lErrorMessage);
                            lRefreshComboBox = true;
                        }
                        else
                        {
                            // no error (and files are not locked) but also nothing changed
                            if (!String.IsNullOrEmpty(lBackupTask.LastBackupErrorMessage))
                            {
                                lBackupTask.LastBackupErrorMessage = "";
                                lRefreshComboBox = true;
                            }
                        }

                        // backup was updated or there was no change that is important for us
                        // if error then try again latter (do not reset FilesChanged flag)
                        if (!lErrorOccurred)
                        {
                            lBackupTask.SourceFilesChanged = false;
                        }
                    }
                }

                // check flag from DestinationFileWatcher
                if (lBackupTask.DestinationDirectoryChanged)
                {
                    string lErrorMessage = "";
                    // to check if Size was changed
                    long lLastDestinationDirectorySize = lBackupTask.DestinationDirectorySize;

                    // backup was made so we need to update destination directory size once more
                    fBackupMaker.RefreshDestinationDirectorySize(lBackupTask);
                    // if destination directory size limit is set then remove older files
                    fBackupMaker.CleanUpOldBackups(lBackupTask, ref lErrorMessage);

                    lBackupTask.DestinationDirectoryChanged = false;

                    // if size is same as before skip updating
                    lItemUpdateNeeded = (lLastDestinationDirectorySize != lBackupTask.DestinationDirectorySize);
                }

                // if we made a backup of currently selected item we need to refresh data on GUI
                if (fSelectedBackupTaskIndex == lIndex)
                {
                    // check if something change or maybe we updated destination directory size
                    if (lItemUpdateNeeded)
                    {
                        SelectTaskByIndex(lIndex, false); // reselect same item, refresh interface
                    }
                }
            }

            fTasksCheckedAtLeastOnce = true;

            // Last Backup Time of some item changed, refresh list
            if (lRefreshComboBox)
            {
                FillComboBoxItems();
            }

            // save settings if something changed
            if (fSettingsChangeCounter != lSettingsChangeCounter)
            {
                if (fSettingsChangeCounter != -1)
                {
                    fSettings.SaveConfig();
                }
                fSettingsChangeCounter = lSettingsChangeCounter;
            }
        }
        public MainWindow()
        {
            InitializeComponent();

            // check if application is running only in one instance if not then focuse on old instance and exit
            fApplicationInstanceManager = new ApplicationInstanceManager();
            CreateTypicalLocationsMenu();


            fBackupMaker = new BackupMaker();

            fSettings = new SettingsManager();
            fSettings.LoadConfig();

            // set window position
            if (fSettings.Settings.Window.PositionKnown)
            {
                this.WindowState = WindowState.Normal;
                this.Top         = fSettings.Settings.Window.Top;
                this.Left        = fSettings.Settings.Window.Left;
                this.Height      = fSettings.Settings.Window.Height;
                this.Width       = fSettings.Settings.Window.Width;

                // done in event 'Window_Loaded()' because window must finish its initialization before setting this flag
                //if (fSettings.Settings.WindowMaximised)
                //    WindowState = WindowState.Maximized;
            }

            // if app is set up to run in tray than create tray icon and minimize
            if (App.fCommandLineOptions.StartInTray)
            {
                ShowInTaskbar = false;

                fTray = new TaskbarIcon();
                //fTray.Icon = Properties.Resources.TrayIcon; // UpdateGlobalStatus will set correct Icon
                fTray.ToolTipText           = this.Title;
                fTray.TrayMouseDoubleClick += (EventSender, EventArgs) => { Show(); WindowState = WindowState.Normal; };

                fTray.ContextMenu = new ContextMenu();
                MenuItem lCloseMenuItem = new MenuItem();
                lCloseMenuItem.Header = "Close";
                lCloseMenuItem.Click += (EventSender, EventArgs) => { Application.Current.Shutdown(); };
                fTray.ContextMenu.Items.Add(lCloseMenuItem);
                fTray.MenuActivation = PopupActivationMode.RightClick;

                WindowState = WindowState.Minimized;
                Hide();
            }

            // referesh destination directory size
            foreach (BackupTask lBackupTask in fSettings.Settings.BackupTasks)
            {
                if (lBackupTask.DestinationDirectorySize == -1)
                {
                    fBackupMaker.RefreshDestinationDirectorySize(lBackupTask);
                }
            }

            FillComboBoxItems();
            SelectTaskByIndex(fSettings.Settings.LastSelectedTaskIndex, false);

            // fill components tooltips
            SetToolTips();

            fTimer           = new Timer(10000);
            fTimer.Elapsed  += HandleTimer;
            fTimer.AutoReset = true;
            fTimer.Start();
            fTimer.Enabled = true;
        }