0

For console Application, when I execute window.Show(), my WPF Window is hang. But When running in App.xaml (Window Application), it is OK.

I use AggregateCatalog to initialize my objects, instead of using codes below.

var application = new System.Windows.Application();
application.Run(new MyWindow());

For AggregateCatalog:

private static AggregateCatalog catalog;
private static CompositionContainer container;
private static IEnumerable<IModuleController> moduleControllers;

private static void RunApplication()
    {
        catalog = new AggregateCatalog();
        catalog.Catalogs.Add(new AssemblyCatalog(typeof(ViewModel).Assembly));
        catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
        container = new CompositionContainer(catalog, CompositionOptions.DisableSilentRejection);
        CompositionBatch batch = new CompositionBatch();
        batch.AddExportedValue(container);
        container.Compose(batch);
        moduleControllers = container.GetExportedValues<IModuleController>();
        foreach (IModuleController moduleController in moduleControllers) { moduleController.Initialize(); }
        foreach (IModuleController moduleController in moduleControllers) { moduleController.Run(); }
    }

Is there limitation if I run it in console application using the same RunApplication()?

king jia
  • 692
  • 8
  • 20
  • 43
  • Calling `Show()` blocks the thread. Use multithreading. See [this](http://stackoverflow.com/a/33239090/4546874). It is WinForms but still relevant. – Fᴀʀʜᴀɴ Aɴᴀᴍ Nov 30 '15 at 10:39
  • Are you talking about a console project (= no interface) or about a WPF project (= mostly interface)? – varocarbas Nov 30 '15 at 10:41
  • a WPF project that run in console Application. – king jia Nov 30 '15 at 10:43
  • The "run in console application" bit seems the most problematic one here, could you please elaborate more about what you mean with that? (it might also be interesting to know about the reasons for this duplicity). – varocarbas Nov 30 '15 at 10:45

1 Answers1

0

Using a console application for showing a windows form is generally not a good idea. There is a good question on stack overflow which gives the exact difference between a console and windows application and what problems you can run into Difference between Windows and Console application

You may be able to run the WPF app by running it in a separate thread but it may still behave in a weird manner , and you as a programmer will have to sort out the issues instead of VS doing it for you when you create a project template which is fit for purpose. Have a look at this comment to understand what I mean : https://stackoverflow.com/a/574934/2794980

Community
  • 1
  • 1
Pratik
  • 868
  • 1
  • 9
  • 24