Browse Source

Reduce number of possible hiccups during MEF initialization by reducing the number of Task.GetAwaiter().GetResult() calls.

pull/2046/head
Siegfried Pammer 5 years ago
parent
commit
789b4e35e5
  1. 38
      ILSpy/App.xaml.cs

38
ILSpy/App.xaml.cs

@ -40,19 +40,12 @@ namespace ICSharpCode.ILSpy @@ -40,19 +40,12 @@ namespace ICSharpCode.ILSpy
/// </summary>
public partial class App : Application
{
internal static CommandLineArguments CommandLineArguments;
internal static readonly IList<ExceptionData> StartupExceptions = new List<ExceptionData>();
static ExportProvider exportProvider;
public static ExportProvider ExportProvider => exportProvider;
public static ExportProvider ExportProvider { get; private set; }
public static IExportProviderFactory ExportProviderFactory { get; private set; }
static IExportProviderFactory exportProviderFactory;
public static IExportProviderFactory ExportProviderFactory => exportProviderFactory;
internal static readonly IList<ExceptionData> StartupExceptions = new List<ExceptionData>();
internal class ExceptionData
{
public Exception Exception;
@ -77,7 +70,16 @@ namespace ICSharpCode.ILSpy @@ -77,7 +70,16 @@ namespace ICSharpCode.ILSpy
Dispatcher.CurrentDispatcher.UnhandledException += Dispatcher_UnhandledException;
}
TaskScheduler.UnobservedTaskException += DotNet40_UnobservedTaskException;
InitializeMef().GetAwaiter().GetResult();
Languages.Initialize(ExportProvider);
EventManager.RegisterClassHandler(typeof(Window),
Hyperlink.RequestNavigateEvent,
new RequestNavigateEventHandler(Window_RequestNavigate));
ILSpyTraceListener.Install();
}
private static async Task InitializeMef()
{
// Cannot show MessageBox here, because WPF would crash with a XamlParseException
// Remember and show exceptions in text output, once MainWindow is properly initialized
try {
@ -93,7 +95,7 @@ namespace ICSharpCode.ILSpy @@ -93,7 +95,7 @@ namespace ICSharpCode.ILSpy
var name = Path.GetFileNameWithoutExtension(plugin);
try {
var asm = Assembly.Load(name);
var parts = discovery.CreatePartsAsync(asm).GetAwaiter().GetResult();
var parts = await discovery.CreatePartsAsync(asm);
catalog = catalog.AddParts(parts);
} catch (Exception ex) {
StartupExceptions.Add(new ExceptionData { Exception = ex, PluginName = name });
@ -101,28 +103,22 @@ namespace ICSharpCode.ILSpy @@ -101,28 +103,22 @@ namespace ICSharpCode.ILSpy
}
}
// Add the built-in parts
catalog = catalog.AddParts(discovery.CreatePartsAsync(Assembly.GetExecutingAssembly()).GetAwaiter().GetResult());
var createdParts = await discovery.CreatePartsAsync(Assembly.GetExecutingAssembly());
catalog = catalog.AddParts(createdParts);
// If/When the project switches to .NET Standard/Core, this will be needed to allow metadata interfaces (as opposed
// to metadata classes). When running on .NET Framework, it's automatic.
// catalog.WithDesktopSupport();
// If/When any part needs to import ICompositionService, this will be needed:
// catalog.WithCompositionService();
var config = CompositionConfiguration.Create(catalog);
exportProviderFactory = config.CreateExportProviderFactory();
exportProvider = exportProviderFactory.CreateExportProvider();
ExportProviderFactory = config.CreateExportProviderFactory();
ExportProvider = ExportProviderFactory.CreateExportProvider();
// This throws exceptions for composition failures. Alternatively, the configuration's CompositionErrors property
// could be used to log the errors directly. Used at the end so that it does not prevent the export provider setup.
config.ThrowOnErrors();
} catch (Exception ex) {
StartupExceptions.Add(new ExceptionData { Exception = ex });
}
Languages.Initialize(exportProvider);
EventManager.RegisterClassHandler(typeof(Window),
Hyperlink.RequestNavigateEvent,
new RequestNavigateEventHandler(Window_RequestNavigate));
ILSpyTraceListener.Install();
}
protected override void OnStartup(StartupEventArgs e)

Loading…
Cancel
Save