public static CustomActivity StartSyncron(string taskname, CustomActivity parentActivity) { CustomActivity dependencyActivity = new CustomActivity(taskname, parentActivity); s_DictionaryStarts.TryAdd(taskname, s_Time.Elapsed); return(dependencyActivity); }
public static async ValueTask <CustomActivity> StartSyncronAsync(string taskname, CustomActivity parentActivity) { CustomActivity dependencyActivity = new CustomActivity(taskname, parentActivity); await s_DictionaryStarts.TryAddAsync(taskname, s_Time.Elapsed); return(dependencyActivity); }
public static CustomActivity StartSyncron(string taskname, CustomActivity parentActivity, CustomActivity.OperationType operationType, string target) { var dependencyActivity = new CustomActivity(taskname, parentActivity, operationType, target); s_DictionaryStarts.TryAdd(taskname, s_Time.Elapsed); return(dependencyActivity); }
private void SetParent(string operationName, CustomActivity parentActivity) { if (parentActivity != null) { MyOperationType = parentActivity.MyOperationType; SetParentId(parentActivity.Id ?? string.Empty); MyTelemetryClient = parentActivity.MyTelemetryClient; MyTelemetryTarget = parentActivity.MyTelemetryTarget; switch (MyOperationType) { case OperationType.DependencyOperation: MyDependencyTelemetry = new DependencyTelemetry(operationName, null, operationName, null, DateTimeOffset.UtcNow, TimeSpan.Zero, "not disposed", true); MyDependencyTelemetry.Context.Operation.ParentId = ParentId; break; case OperationType.RequestOperation: MyRequestTelemetry = new RequestTelemetry(operationName, DateTimeOffset.UtcNow, TimeSpan.Zero, "not disposed", true); MyRequestTelemetry.Context.Operation.ParentId = ParentId; if (!string.IsNullOrEmpty(MyTelemetryTarget) && Uri.TryCreate(MyTelemetryTarget, UriKind.Absolute, out Uri uriResult)) { MyRequestTelemetry.Url = uriResult; } break; default: throw new NotImplementedException("Implement OperationType " + parentActivity.MyOperationType); } } else { throw new ArgumentNullException(nameof(parentActivity), "operationType must be supplied in case of parentActivity == null!"); } }
public CustomActivity(string operationName, CustomActivity parentActivity, OperationType operationType, string target) : base(operationName) { MyOperationType = operationType; MyTelemetryTarget = target; if (parentActivity != null) { SetParent(operationName, parentActivity); Start(); return; } MyTelemetryClient = new TelemetryClient(); Start(); switch (operationType) { case OperationType.DependencyOperation: MyDependencyTelemetry = new DependencyTelemetry(operationName, MyTelemetryTarget, null, null, DateTimeOffset.UtcNow, TimeSpan.Zero, "not disposed", true); MyDependencyTelemetry.Context.Operation.Id = Id; MyTelemetryClient.Context.Operation.Id = MyDependencyTelemetry.Context.Operation.Id; break; case OperationType.RequestOperation: MyRequestTelemetry = new RequestTelemetry(operationName, DateTimeOffset.UtcNow, TimeSpan.Zero, "not disposed", true); MyRequestTelemetry.Context.Operation.Id = Id; MyTelemetryClient.Context.Operation.Id = MyRequestTelemetry.Context.Operation.Id; if (!string.IsNullOrEmpty(MyTelemetryTarget) && Uri.TryCreate(MyTelemetryTarget, UriKind.Absolute, out Uri uriResult)) { MyRequestTelemetry.Url = uriResult; } break; default: throw new NotImplementedException("Implement OperationType " + operationType); } }
public CustomActivity(string operationName, CustomActivity parentActivity) : base(operationName) { SetParent(operationName, parentActivity); Start(); }
private async ValueTask LoadContent() { using (CustomActivity opLoadMasterindex = Timekeeper.StartSyncron("op_load_frm_masterindex", null, CustomActivity.OperationType.RequestOperation, null)) { _dicCachedNotes.Clear(); foreach (MasterIndexEntry objExistingEntry in _lstItems.Select(x => x.Value)) { objExistingEntry.Dispose(); } _lstItems.Clear(); _lstFileNamesWithItems.Clear(); string strSourceFilter; using (new FetchSafelyFromPool <HashSet <string> >(Utils.StringHashSetPool, out HashSet <string> setValidCodes)) { foreach (XPathNavigator xmlBookNode in (await XmlManager.LoadXPathAsync( "books.xml", _objSelectedSetting.EnabledCustomDataDirectoryPaths)) .SelectAndCacheExpression("/chummer/books/book/code")) { setValidCodes.Add(xmlBookNode.Value); } setValidCodes.IntersectWith(_objSelectedSetting.Books); strSourceFilter = setValidCodes.Count > 0 ? '(' + string.Join(" or ", setValidCodes.Select(x => "source = " + x.CleanXPath())) + ')' : "source"; } using (_ = Timekeeper.StartSyncron("load_frm_masterindex_load_andpopulate_entries", opLoadMasterindex)) { ConcurrentBag <ListItem> lstItemsForLoading = new ConcurrentBag <ListItem>(); using (_ = Timekeeper.StartSyncron("load_frm_masterindex_load_entries", opLoadMasterindex)) { ConcurrentBag <ListItem> lstFileNamesWithItemsForLoading = new ConcurrentBag <ListItem>(); // Prevents locking the UI thread while still benefitting from static scheduling of Parallel.ForEach await Task.WhenAll(_astrFileNames.Select(strFileName => Task.Run(async() => { XPathNavigator xmlBaseNode = await XmlManager.LoadXPathAsync(strFileName, _objSelectedSetting.EnabledCustomDataDirectoryPaths); xmlBaseNode = xmlBaseNode.SelectSingleNodeAndCacheExpression("/chummer"); if (xmlBaseNode == null) { return; } bool blnLoopFileNameHasItems = false; foreach (XPathNavigator xmlItemNode in xmlBaseNode.SelectAndCacheExpression( ".//*[page and " + strSourceFilter + ']')) { blnLoopFileNameHasItems = true; string strName = xmlItemNode.SelectSingleNodeAndCacheExpression("name")?.Value; string strDisplayName = xmlItemNode.SelectSingleNodeAndCacheExpression("translate")?.Value ?? strName ?? xmlItemNode.SelectSingleNodeAndCacheExpression("id")?.Value ?? await LanguageManager.GetStringAsync("String_Unknown"); string strSource = xmlItemNode.SelectSingleNodeAndCacheExpression("source")?.Value; string strPage = xmlItemNode.SelectSingleNodeAndCacheExpression("page")?.Value; string strDisplayPage = xmlItemNode.SelectSingleNodeAndCacheExpression("altpage")?.Value ?? strPage; string strEnglishNameOnPage = xmlItemNode.SelectSingleNodeAndCacheExpression("nameonpage")?.Value ?? strName; string strTranslatedNameOnPage = xmlItemNode.SelectSingleNodeAndCacheExpression("altnameonpage")?.Value ?? strDisplayName; string strNotes = xmlItemNode.SelectSingleNodeAndCacheExpression("altnotes")?.Value ?? xmlItemNode.SelectSingleNodeAndCacheExpression("notes")?.Value; MasterIndexEntry objEntry = new MasterIndexEntry( strDisplayName, strFileName, new SourceString(strSource, strPage, GlobalSettings.DefaultLanguage, GlobalSettings.InvariantCultureInfo), new SourceString(strSource, strDisplayPage, GlobalSettings.Language, GlobalSettings.CultureInfo), strEnglishNameOnPage, strTranslatedNameOnPage); lstItemsForLoading.Add(new ListItem(objEntry, strDisplayName)); if (!string.IsNullOrEmpty(strNotes)) { _dicCachedNotes.TryAdd(objEntry, strNotes); } } if (blnLoopFileNameHasItems) { lstFileNamesWithItemsForLoading.Add(new ListItem(strFileName, strFileName)); } }))); _lstFileNamesWithItems.AddRange(lstFileNamesWithItemsForLoading); } using (_ = Timekeeper.StartSyncron("load_frm_masterindex_populate_entries", opLoadMasterindex)) { string strSpace = await LanguageManager.GetStringAsync("String_Space"); string strFormat = "{0}" + strSpace + "[{1}]"; Dictionary <string, List <ListItem> > dicHelper = new Dictionary <string, List <ListItem> >(lstItemsForLoading.Count); try { foreach (ListItem objItem in lstItemsForLoading) { if (!(objItem.Value is MasterIndexEntry objEntry)) { continue; } string strKey = objEntry.DisplayName.ToUpperInvariant(); if (dicHelper.TryGetValue(strKey, out List <ListItem> lstExistingItems)) { ListItem objExistingItem = lstExistingItems.Find( x => x.Value is MasterIndexEntry y && objEntry.DisplaySource.Equals(y.DisplaySource)); if (objExistingItem.Value is MasterIndexEntry objLoopEntry) { objLoopEntry.FileNames.UnionWith(objEntry.FileNames); objEntry.Dispose(); } else { using (new FetchSafelyFromPool <List <ListItem> >( Utils.ListItemListPool, out List <ListItem> lstItemsNeedingNameChanges)) { lstItemsNeedingNameChanges.AddRange(lstExistingItems.FindAll( x => x.Value is MasterIndexEntry y && !objEntry.FileNames.IsSubsetOf(y.FileNames))); if (lstItemsNeedingNameChanges.Count == 0) { _lstItems.Add( objItem); // Not using AddRange because of potential memory issues lstExistingItems.Add(objItem); } else { ListItem objItemToAdd = new ListItem( objItem.Value, string.Format(GlobalSettings.CultureInfo, strFormat, objItem.Name, string.Join( ',' + strSpace, objEntry.FileNames))); _lstItems.Add( objItemToAdd); // Not using AddRange because of potential memory issues lstExistingItems.Add(objItemToAdd); foreach (ListItem objToRename in lstItemsNeedingNameChanges) { _lstItems.Remove(objToRename); lstExistingItems.Remove(objToRename); if (!(objToRename.Value is MasterIndexEntry objExistingEntry)) { continue; } objItemToAdd = new ListItem(objToRename.Value, string.Format( GlobalSettings.CultureInfo, strFormat, objExistingEntry.DisplayName, string.Join( ',' + strSpace, objExistingEntry.FileNames))); _lstItems.Add( objItemToAdd); // Not using AddRange because of potential memory issues lstExistingItems.Add(objItemToAdd); } } } } } else { _lstItems.Add(objItem); // Not using AddRange because of potential memory issues List <ListItem> lstHelperItems = Utils.ListItemListPool.Get(); lstHelperItems.Add(objItem); dicHelper.Add(strKey, lstHelperItems); } } } finally { foreach (List <ListItem> lstHelperItems in dicHelper.Values) { Utils.ListItemListPool.Return(lstHelperItems); } } } } using (_ = Timekeeper.StartSyncron("load_frm_masterindex_sort_entries", opLoadMasterindex)) { _lstItems.Sort(CompareListItems.CompareNames); _lstFileNamesWithItems.Sort(CompareListItems.CompareNames); } using (_ = Timekeeper.StartSyncron("load_frm_masterindex_populate_controls", opLoadMasterindex)) { _lstFileNamesWithItems.Insert(0, new ListItem(string.Empty, await LanguageManager.GetStringAsync("String_All"))); int intOldSelectedIndex = cboFile.SelectedIndex; cboFile.BeginUpdate(); cboFile.PopulateWithListItems(_lstFileNamesWithItems); try { cboFile.SelectedIndex = Math.Max(intOldSelectedIndex, 0); } // For some reason, some unit tests will fire this exception even when _lstFileNamesWithItems is explicitly checked for having enough items catch (ArgumentOutOfRangeException) { cboFile.SelectedIndex = -1; } cboFile.EndUpdate(); lstItems.BeginUpdate(); lstItems.PopulateWithListItems(_lstItems); lstItems.SelectedIndex = -1; lstItems.EndUpdate(); _blnSkipRefresh = false; } } }