예제 #1
0
        private void DoPrint(object sender, DoWorkEventArgs e)
        {
            void FuncIncreaseProgress()
            {
                prgProgress.Value += 1;
            }

            Character[] lstCharacters = new Character[treCharacters.Nodes.Count];
            for (int i = 0; i < lstCharacters.Length; ++i)
            {
                if (_workerPrinter.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                lstCharacters[i] = new Character
                {
                    FileName = treCharacters.Nodes[i].Tag.ToString()
                };
            }

            CancellationTokenSource objCancellationTokenSource = new CancellationTokenSource();

            // Because we're printing info from characters here and we don't have locker objects assigned to characters to prevent changes while a print method is running,
            // we cannot make this method async, but we still do not want Parallel.ForEach to block UI events
            Utils.RunWithoutThreadLock(() =>
            {
                // Parallelized load because this is one major bottleneck.
                Parallel.ForEach(lstCharacters, (objCharacter, objState) =>
                {
                    if (_workerPrinter.CancellationPending || objState.ShouldExitCurrentIteration || objCancellationTokenSource.IsCancellationRequested)
                    {
                        if (!objState.IsStopped)
                        {
                            objState.Stop();
                        }
                        objCancellationTokenSource.Cancel();
                        return;
                    }

                    bool blnLoadSuccessful = objCharacter.Load();
                    if (_workerPrinter.CancellationPending || objState.ShouldExitCurrentIteration || objCancellationTokenSource.IsCancellationRequested)
                    {
                        if (!objState.IsStopped)
                        {
                            objState.Stop();
                        }
                        objCancellationTokenSource.Cancel();
                        return;
                    }

                    if (blnLoadSuccessful)
                    {
                        prgProgress.Invoke((Action)FuncIncreaseProgress);
                    }
                });
            });
            if (_workerPrinter.CancellationPending)
            {
                e.Cancel = true;
            }
            else
            {
                if (_lstCharacters?.Count > 0)
                {
                    foreach (Character objCharacter in _lstCharacters)
                    {
                        objCharacter.Dispose();
                    }
                }
                _lstCharacters = new List <Character>(lstCharacters);
            }
        }
예제 #2
0
 public static ThreadSafeForm <T> Get(Func <T> funcFormConstructor)
 {
     return(new ThreadSafeForm <T>(Program.MainForm != null
                                      ? Program.MainForm.DoThreadSafeFunc(funcFormConstructor)
                                      : Utils.RunWithoutThreadLock(() => Utils.StartStaTask(funcFormConstructor))));
 }
예제 #3
0
        private static void LoadCharacterSettings()
        {
            _intDicLoadedCharacterSettingsLoadedStatus = 0;
            try
            {
                s_DicLoadedCharacterSettings.Clear();
                try
                {
                    if (Utils.IsDesignerMode || Utils.IsRunningInVisualStudio)
                    {
                        CharacterSettings objNewCharacterSettings = new CharacterSettings();
                        if (!s_DicLoadedCharacterSettings.TryAdd(GlobalSettings.DefaultCharacterSetting,
                                                                 objNewCharacterSettings))
                        {
                            objNewCharacterSettings.Dispose();
                        }
                        return;
                    }

                    Utils.RunWithoutThreadLock(async() =>
                    {
                        IEnumerable <XPathNavigator> xmlSettingsIterator
                            = (await(await XmlManager.LoadXPathAsync("settings.xml"))
                               .SelectAndCacheExpressionAsync("/chummer/settings/setting")).Cast <XPathNavigator>();
                        Parallel.ForEach(xmlSettingsIterator, xmlBuiltInSetting =>
                        {
                            CharacterSettings objNewCharacterSettings = new CharacterSettings();
                            if (!objNewCharacterSettings.Load(xmlBuiltInSetting) ||
                                (objNewCharacterSettings.BuildMethodIsLifeModule &&
                                 !GlobalSettings.LifeModuleEnabled) ||
                                !s_DicLoadedCharacterSettings.TryAdd(objNewCharacterSettings.DictionaryKey,
                                                                     objNewCharacterSettings))
                            {
                                objNewCharacterSettings.Dispose();
                            }
                        });
                    });
                }
                finally
                {
                    Interlocked.Increment(ref _intDicLoadedCharacterSettingsLoadedStatus);
                }

                Utils.RunWithoutThreadLock(() =>
                {
                    string strSettingsPath = Path.Combine(Utils.GetStartupPath, "settings");
                    if (Directory.Exists(strSettingsPath))
                    {
                        Parallel.ForEach(Directory.EnumerateFiles(strSettingsPath, "*.xml"), strSettingsFilePath =>
                        {
                            string strSettingName = Path.GetFileName(strSettingsFilePath);
                            CharacterSettings objNewCharacterSettings = new CharacterSettings();
                            if (!objNewCharacterSettings.Load(strSettingName, false) ||
                                (objNewCharacterSettings.BuildMethodIsLifeModule &&
                                 !GlobalSettings.LifeModuleEnabled) ||
                                !s_DicLoadedCharacterSettings.TryAdd(objNewCharacterSettings.DictionaryKey,
                                                                     objNewCharacterSettings))
                            {
                                objNewCharacterSettings.Dispose();
                            }
                        });
                    }
                });
            }
            finally
            {
                Interlocked.Increment(ref _intDicLoadedCharacterSettingsLoadedStatus);
            }
        }