public static TimeSpan Finish(string taskname) { TimeSpan final = TimeSpan.Zero; if (s_DictionaryStarts.TryRemove(taskname, out TimeSpan objStartTimeSpan)) { final = s_Time.Elapsed - objStartTimeSpan; #if DEBUG string strLogEntry = string.Format(GlobalSettings.InvariantCultureInfo, "Task \"{0}\" finished in {1}", taskname, final); //Log.Trace(strLogEntry); Debug.WriteLine(strLogEntry); #endif s_DictionaryStatistics.AddOrUpdate(taskname, x => new Tuple <TimeSpan, int>(final, 1), (x, y) => new Tuple <TimeSpan, int>( y.Item1 + final, y.Item2 + 1)); } else { Debug.WriteLine("Non started task \"" + taskname + "\" finished"); } return(final); }
/// <summary> /// Get the compiled Xsl Transform of an Xsl file. Will throw exceptions if anything goes awry. /// If we've already compiled the same Xsl Transform before, we'll fetch the cached version of that transform instead of repeating it. /// Uses flag hack method design outlined here to avoid locking: /// https://docs.microsoft.com/en-us/archive/msdn-magazine/2015/july/async-programming-brownfield-async-development /// </summary> /// <param name="blnSync">Flag for whether method should always use synchronous code or not.</param> /// <param name="strXslFilePath">Absolute path to the Xsl file to be transformed.</param> /// <returns>The compiled Xsl transform of <paramref name="strXslFilePath"/>.</returns> private static async Task <XslCompiledTransform> GetTransformForFileCoreAsync(bool blnSync, string strXslFilePath) { if (!File.Exists(strXslFilePath)) { throw new FileNotFoundException(nameof(strXslFilePath)); } DateTime datLastWriteTimeUtc = File.GetLastWriteTimeUtc(strXslFilePath); XslCompiledTransform objReturn; bool blnSuccess; Tuple <DateTime, XslCompiledTransform> tupCachedData; if (blnSync) { // ReSharper disable once MethodHasAsyncOverload blnSuccess = s_dicCompiledTransforms.TryGetValue(strXslFilePath, out tupCachedData); } else { (blnSuccess, tupCachedData) = await s_dicCompiledTransforms.TryGetValueAsync(strXslFilePath); } if (!blnSuccess || tupCachedData.Item1 <= datLastWriteTimeUtc) { #if DEBUG objReturn = new XslCompiledTransform(true); #else objReturn = new XslCompiledTransform(); #endif if (blnSync) { objReturn.Load(strXslFilePath); Tuple <DateTime, XslCompiledTransform> tupNewValue = new Tuple <DateTime, XslCompiledTransform>(datLastWriteTimeUtc, objReturn); // ReSharper disable once MethodHasAsyncOverload s_dicCompiledTransforms.AddOrUpdate(strXslFilePath, tupNewValue, (x, y) => tupNewValue); } else { await Task.Run(() => objReturn.Load(strXslFilePath)); Tuple <DateTime, XslCompiledTransform> tupNewValue = new Tuple <DateTime, XslCompiledTransform>(datLastWriteTimeUtc, objReturn); await s_dicCompiledTransforms.AddOrUpdateAsync(strXslFilePath, tupNewValue, (x, y) => tupNewValue); } } else { objReturn = tupCachedData.Item2; } return(objReturn); }
private void SetTextToWorkerResult(string strText) { string strDisplayText = strText; // Displayed text has all mugshots data removed because it's unreadable as Base64 strings, but massie enough to slow down the program strDisplayText = Regex.Replace(strDisplayText, "<mainmugshotbase64>[^\\s\\S]*</mainmugshotbase64>", "<mainmugshotbase64>[...]</mainmugshotbase64>"); strDisplayText = Regex.Replace(strDisplayText, "<stringbase64>[^\\s\\S]*</stringbase64>", "<stringbase64>[...]</stringbase64>"); strDisplayText = Regex.Replace(strDisplayText, "base64\": \"[^\\\"]*\",", "base64\": \"[...]\","); _dicCache.AddOrUpdate(new Tuple <string, string>(_strExportLanguage, _strXslt), new Tuple <string, string>(strText, strDisplayText), (a, b) => new Tuple <string, string>(strText, strDisplayText)); txtText.DoThreadSafe(() => txtText.Text = strDisplayText); }
private Task SetTextToWorkerResult(string strText, CancellationToken token = default) { string strDisplayText = strText; // Displayed text has all mugshots data removed because it's unreadable as Base64 strings, but massive enough to slow down the program strDisplayText = s_RgxMainMugshotReplaceExpression.Replace(strDisplayText, "<mainmugshotbase64>[...]</mainmugshotbase64>"); strDisplayText = s_RgxStringBase64ReplaceExpression.Replace(strDisplayText, "<stringbase64>[...]</stringbase64>"); strDisplayText = s_RgxBase64ReplaceExpression.Replace(strDisplayText, "base64\": \"[...]\","); _dicCache.AddOrUpdate(new Tuple <string, string>(_strExportLanguage, _strXslt), new Tuple <string, string>(strText, strDisplayText), (a, b) => new Tuple <string, string>(strText, strDisplayText)); return(txtText.DoThreadSafeAsync(x => x.Text = strDisplayText, token)); }