public static void Unload(AppDomain domain) { var assembly = domain.GetAssemblies()[1]; var filePath = assembly.CodeBase; Commands.Where(c => assembly == c.GetType().Assembly).ToList().ForEach(c => Commands.Remove(c)); EventConsumers.Where(c => assembly == c.GetType().Assembly).ToList().ForEach(c => EventConsumers.Remove(c)); EventSources.Where(c => assembly == c.GetType().Assembly).ToList().ForEach(c => EventSources.Remove(c)); Services.Where(c => assembly == c.GetType().Assembly).ToList().ForEach(c => { c.Stop(); Services.Remove(c); }); Bindings.Where(c => assembly == c.GetType().Assembly).ToList().ForEach(c => Bindings.Remove(c)); Domains.Remove(domain); AppDomain.Unload(domain); GC.Collect(); // collects all unused memory GC.WaitForPendingFinalizers(); // wait until GC has finished its work GC.Collect(); //File.Delete(filePath); }
public async Task GenerateAsync() { var selectedEventSourceIds = EventSources.Where(p => p.IsSelected) .SelectMany(p => p.ServiceIds) .ToList(); var selectedReportingSinkTypes = ReportingSinks.Where(p => p.IsSelected) .SelectMany(p => p.ServiceIds) .ToList(); var selectedStartLocalTime = m_startLocalTime; var selectedEndLocalTime = m_endLocalTime; var selectedIsGeneratePerSourceEnabled = m_isGeneratePerSourceEnabled; var selectedIsGeneratePerSummaryEnabled = m_isGenerateSummaryEnabled; if (selectedEventSourceIds.Any() && selectedReportingSinkTypes.Any() && (selectedIsGeneratePerSourceEnabled || selectedIsGeneratePerSummaryEnabled)) { ProgressPercentage = 0; IsBusy = true; ProgressPercentage = 1; ProgressStatus = String.Empty; string currentActivity = string.Empty; var uiDispatcher = Dispatcher.CurrentDispatcher; await Task.Run(() => { try { var eventQueryServiceRegistrations = m_eventQueryServices .Where(p => selectedEventSourceIds.Contains(p.Key.Id)); var renderServiceRegistrations = m_renderServices .Where(p => selectedReportingSinkTypes.Contains(p.Key.Id)); var eventQueryServiceRegistrationsCount = eventQueryServiceRegistrations.Count(); var totalProgressSteps = eventQueryServiceRegistrationsCount; if (selectedIsGeneratePerSourceEnabled) { totalProgressSteps += eventQueryServiceRegistrationsCount; } if (selectedIsGeneratePerSummaryEnabled) { totalProgressSteps += eventQueryServiceRegistrationsCount; // uhh nice padding for summary generation } var progressIncrement = 100 / Math.Max(totalProgressSteps, 1); var processingResults = new List <ProcessingResult>(); var cancelSource = new CancellationTokenSource(); var tf = new TaskFactory(); var taskList = new List <Task>(); foreach (var eventQueryServiceRegistration in eventQueryServiceRegistrations) { if (m_isParallelProcessingEnabled) { var task = tf.StartNew(() => { if (cancelSource.IsCancellationRequested) { return; } var result = ProcessResult(eventQueryServiceRegistration, uiDispatcher, selectedStartLocalTime, selectedEndLocalTime, progressIncrement); if (result.Exception != null) { m_messenger.Send(result.Exception); cancelSource.Cancel(); return; } processingResults.Add(result); }, cancelSource.Token); taskList.Add(task); } else { var result = ProcessResult(eventQueryServiceRegistration, uiDispatcher, selectedStartLocalTime, selectedEndLocalTime, progressIncrement); if (result.Exception != null) { m_messenger.Send(result.Exception); cancelSource.Cancel(); return; } processingResults.Add(result); } } Task.WaitAll(taskList.ToArray()); if (cancelSource.IsCancellationRequested) { return; } if (selectedIsGeneratePerSourceEnabled) { foreach (var result in processingResults) { foreach (var render in renderServiceRegistrations) { KeyValuePair <ServiceRegistration, IRenderEvents> render1 = render; currentActivity = String.Format("Rendering data for {0} - {1} with {2} - {3}...", result.EventSourceService.Family, result.EventSourceService.Name, render1.Key.Family, render1.Key.Name); uiDispatcher.Invoke( () => { ProgressStatus = String.Format("{0}...", currentActivity); }); Action renderEventsDelegate = () => render1.Value.Render(result.EventSourceService.Id, selectedStartLocalTime, (selectedEndLocalTime.AddDays(1).AddTicks(-1)), result.Events, result.WeightedTags, result.WeightedPeople, result.ImportantSentences); if (render.Key.InvokeOnShellDispatcher) { uiDispatcher.Invoke(renderEventsDelegate); } else { renderEventsDelegate(); } } } } if (selectedIsGeneratePerSummaryEnabled) { var summaryEvents = new List <Event>(); var summaryWeightedTags = new ConcurrentDictionary <string, int>(); var summaryWeightedPeople = new ConcurrentDictionary <string, int>(); var summaryImportantSentences = new List <string>(); foreach (var result in processingResults) { summaryEvents.AddRange(result.Events); foreach (var weightedTagEntry in result.WeightedTags) { summaryWeightedTags.AddOrUpdate(weightedTagEntry.Key, weightedTagEntry.Value, (s, i) => i + weightedTagEntry.Value); } foreach (var weightedPersonEntry in result.WeightedPeople) { summaryWeightedPeople.AddOrUpdate(weightedPersonEntry.Key, weightedPersonEntry.Value, (s, i) => i + weightedPersonEntry.Value); } summaryImportantSentences.AddRange(result.ImportantSentences); } foreach (var render in renderServiceRegistrations) { KeyValuePair <ServiceRegistration, IRenderEvents> render1 = render; Action renderEventsDelegate = () => render1.Value.Render("Summary", selectedStartLocalTime, (selectedEndLocalTime.AddDays(1).AddTicks(-1)), summaryEvents, summaryWeightedTags, summaryWeightedPeople, summaryImportantSentences); currentActivity = String.Format("Rendering summary data with {0} - {1}", render1.Key.Family, render1.Key.Name); uiDispatcher.Invoke(() => { ProgressStatus = String.Format("{0}...", currentActivity); }); if (render.Key.InvokeOnShellDispatcher) { uiDispatcher.Invoke(renderEventsDelegate); } else { renderEventsDelegate(); } } } uiDispatcher.Invoke(() => { ProgressPercentage = 100; }); } catch (AggregateException ex) { Trace.WriteLine("Aggregate inner exception: " + ex.InnerException); m_messenger.Send(new ExceptionMessage(ex.InnerException, currentActivity)); } catch (Exception ex) { Trace.WriteLine(ex); m_messenger.Send(new ExceptionMessage(ex, currentActivity)); } }); IsBusy = false; ProgressStatus = "...done."; ProgressPercentage = 100; } }