private void RestoreThread(object p)
        {
            var vm = p as MainViewModel;
            this.Dispatcher.BeginInvoke(new Action(delegate()
            {
                vm.IsBusy = true;
            }));
            var proc = new BackupProcessor();
            var param = new BackupProcessor.BackupActionParameter();
            param.OutputFilePath = vm.OutputFilePath;
            param.DeviceTempFolderPath = BackupProcessor.DeviceTempFolderPath;

            param.TempFolderPath = BackupProcessor.TempFolderPath;
            param.SourceFilePath = vm.CurrentFilePath;
            param.SourceFolderPath = BackupProcessor.SourceFolderPath;

            param.RegistryStore = new RegistryStore();
            proc.ActionSettings = vm.ActionSettings;
            Action<BackupProcessor.CancellationStateCheck> cancellationCheck = new Action<BackupProcessor.CancellationStateCheck>(delegate(BackupProcessor.CancellationStateCheck check)
            {
                lock (CancellationPendingLock)
                {
                    check.isCancelled = CancellationPending;
                }
            });

            param.CheckCancellationState = cancellationCheck;
            int count = 0, currentAction = 0;
            for (int i = 0; i < BackupProcessor.RestoreActions.Count; ++i)
            {
                var action = BackupProcessor.RestoreActions[i];
                if (String.IsNullOrEmpty(action.SettingName) || (proc.ActionSettings.ContainsKey(action.SettingName) && proc.ActionSettings[action.SettingName].IsEnabled))
                {
                    count++;
                }
            }
            bool cancelled = false;
            string errorText = null;
            for (int i = 0; i < BackupProcessor.RestoreActions.Count; ++i)
            {
                lock (CancellationPendingLock)
                {
                    if (CancellationPending)
                    {
                        cancelled = true;
                        break;
                    }
                }

                var action = BackupProcessor.RestoreActions[i];
                if (String.IsNullOrEmpty(action.SettingName) || (proc.ActionSettings.ContainsKey(action.SettingName) && proc.ActionSettings[action.SettingName].IsEnabled))
                {
                    param.IsCancelledByAction = false;
                    param.Error = null;
                    if (proc.ActionSettings.ContainsKey(action.SettingName))
                        param.Setting = proc.ActionSettings[action.SettingName];
                    else
                        param.Setting = null;
                    this.Dispatcher.BeginInvoke(new Action(delegate()
                    {
                        vm.ProgressText = action.Name;
                        double val = 100 / count * currentAction;
                        val = Math.Floor(val);
                        vm.ProgressValue = (int)val;
                    }));
                    try
                    {
                        action.Execute(param);
                        if (param.IsCancelledByAction)
                        {
                            cancelled = true;
                            break;
                        }
                        if (param.Error != null)
                        {
                            errorText = param.Error;
                            break;
                        }
                    }
                    catch (OpenNETCF.Desktop.Communication.RAPIException ex)
                    {
                        errorText = ex.ToString();
                        break;
                    }
                    currentAction++;
                }

            }
            this.Dispatcher.BeginInvoke(new Action(delegate()
            {
                vm.ProgressText = LocalizedResources.Finished;
                vm.ProgressValue = 100;
                if (errorText != null)
                {
                    var pg = new pageError(ViewModel);
                    pg.SetErrorDescText(errorText);
                    ViewModel.Navigate(pg);
                }
                else
                {
                    if (!cancelled)
                    {
                        var pg = new pageSuccess(ViewModel);
                        pg.IsBackupMode = false;
                        ViewModel.Navigate(pg);
                    }
                    else
                    {
                        var pg = new pageCancelled(ViewModel);
                        pg.IsBackupMode = false;
                        ViewModel.Navigate(pg);
                    }
                }
                vm.IsBusy = false;
            }));
        }
        private void RestoreThread(object p)
        {
            var vm = p as MainViewModel;

            this.Dispatcher.BeginInvoke(new Action(delegate()
            {
                vm.IsBusy = true;
            }));
            var proc  = new BackupProcessor();
            var param = new BackupProcessor.BackupActionParameter();

            param.OutputFilePath       = vm.OutputFilePath;
            param.DeviceTempFolderPath = BackupProcessor.DeviceTempFolderPath;

            param.TempFolderPath   = BackupProcessor.TempFolderPath;
            param.SourceFilePath   = vm.CurrentFilePath;
            param.SourceFolderPath = BackupProcessor.SourceFolderPath;

            param.RegistryStore = new RegistryStore();
            proc.ActionSettings = vm.ActionSettings;
            Action <BackupProcessor.CancellationStateCheck> cancellationCheck = new Action <BackupProcessor.CancellationStateCheck>(delegate(BackupProcessor.CancellationStateCheck check)
            {
                lock (CancellationPendingLock)
                {
                    check.isCancelled = CancellationPending;
                }
            });

            param.CheckCancellationState = cancellationCheck;
            int count = 0, currentAction = 0;

            for (int i = 0; i < BackupProcessor.RestoreActions.Count; ++i)
            {
                var action = BackupProcessor.RestoreActions[i];
                if (String.IsNullOrEmpty(action.SettingName) || (proc.ActionSettings.ContainsKey(action.SettingName) && proc.ActionSettings[action.SettingName].IsEnabled))
                {
                    count++;
                }
            }
            bool   cancelled = false;
            string errorText = null;

            for (int i = 0; i < BackupProcessor.RestoreActions.Count; ++i)
            {
                lock (CancellationPendingLock)
                {
                    if (CancellationPending)
                    {
                        cancelled = true;
                        break;
                    }
                }

                var action = BackupProcessor.RestoreActions[i];
                if (String.IsNullOrEmpty(action.SettingName) || (proc.ActionSettings.ContainsKey(action.SettingName) && proc.ActionSettings[action.SettingName].IsEnabled))
                {
                    param.IsCancelledByAction = false;
                    param.Error = null;
                    if (proc.ActionSettings.ContainsKey(action.SettingName))
                    {
                        param.Setting = proc.ActionSettings[action.SettingName];
                    }
                    else
                    {
                        param.Setting = null;
                    }
                    this.Dispatcher.BeginInvoke(new Action(delegate()
                    {
                        vm.ProgressText  = action.Name;
                        double val       = 100 / count * currentAction;
                        val              = Math.Floor(val);
                        vm.ProgressValue = (int)val;
                    }));
                    try
                    {
                        action.Execute(param);
                        if (param.IsCancelledByAction)
                        {
                            cancelled = true;
                            break;
                        }
                        if (param.Error != null)
                        {
                            errorText = param.Error;
                            break;
                        }
                    }
                    catch (OpenNETCF.Desktop.Communication.RAPIException ex)
                    {
                        errorText = ex.ToString();
                        break;
                    }
                    currentAction++;
                }
            }
            this.Dispatcher.BeginInvoke(new Action(delegate()
            {
                vm.ProgressText  = LocalizedResources.Finished;
                vm.ProgressValue = 100;
                if (errorText != null)
                {
                    var pg = new pageError(ViewModel);
                    pg.SetErrorDescText(errorText);
                    ViewModel.Navigate(pg);
                }
                else
                {
                    if (!cancelled)
                    {
                        var pg          = new pageSuccess(ViewModel);
                        pg.IsBackupMode = false;
                        ViewModel.Navigate(pg);
                    }
                    else
                    {
                        var pg          = new pageCancelled(ViewModel);
                        pg.IsBackupMode = false;
                        ViewModel.Navigate(pg);
                    }
                }
                vm.IsBusy = false;
            }));
        }