Application.Current "null" in console application

13,613

Solution 1

Just to offer an alternative solution. It is possible to keep an application running without any windows open. To me this feels less 'hackish'. :) http://msdn.microsoft.com/en-us/library/system.windows.application.shutdownmode.aspx

public class AppCode : Application
{
   // Entry point method
   [STAThread]
   public static void Main()
   {
      AppCode app = new AppCode();
      app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
      app.Run();
      ...
      app.Shutdown();
   }
}

EDIT: Ok this got a bit cumbersome. Application.Run will block, so it needs to run in its own thread. When it does run in its own thread, any interaction between your main thread and your ui thread had best be done by Application.Current.Dispatcher.Invoke. Here is some working code, that assumes you have a class that inherits from Application. I'm using a modified App.xaml/App.xaml.cs that a WPF project template creates for you, to get nice handling of ResourceDictionaries for free.

public class Program
{
  // Entry point method
  [STAThread]
  public static void Main()
  {
     var thread = new System.Threading.Thread(CreateApp);
     thread.SetApartmentState(System.Threading.ApartmentState.STA);
     thread.Start();

     // This is kinda shoddy, but the thread needs some time 
     // before we can invoke anything on the dispatcher
     System.Threading.Thread.Sleep(100);

     // In order to get input from the user, display a
     // dialog and return the result on the dispatcher
     var result = (int)Application.Current.Dispatcher.Invoke(new Func<int>(() =>
        {
           var win = new MainWindow();
           win.ShowDialog();
           return 10;
        }), null);

     // Show something to the user without waiting for a result
     Application.Current.Dispatcher.Invoke(new Action(() =>
     {
        var win = new MainWindow();
        win.ShowDialog();
     }), null);

     System.Console.WriteLine("result" + result);
     System.Console.ReadLine();

     // This doesn't really seem necessary 
     Application.Current.Dispatcher.InvokeShutdown();
  }

  private static void CreateApp()
  {
     App app = new App();
     app.ShutdownMode = ShutdownMode.OnExplicitShutdown;
     app.Run();
  }
}

Solution 2

The following is the intended behavior of Application class:

  • The first open window is the MainWindow.
  • The only window in the list becomes the MainWindow (if others are to be removed).
  • Application Class is designed to exit if no windows are present in windows list.

Check this link.

So basically you cannot run an Application, without any window open. Keep a window open but hidden.


If I have misunderstood your problem, then perhaps the following similar cases might help:

Share:
13,613
em70
Author by

em70

Updated on June 03, 2022

Comments

  • em70
    em70 almost 2 years

    I'm currently trying to use a WPF component that makes use of Application.Current from a WPF application, however due to several reasons I never call Application.Run (nor is that an option). The result is a NullReferenceException.

    I'm basically trying to display multiple instances of the same WPF window from what would be a console application. Any advice (and code samples in C#/F#) would be welcome!

    Thanks in advance

  • em70
    em70 over 12 years
    I ended up running as you suggested a hidden window. In the context of that window, via Dispatcher.Invoke calls I instantiate and .Show() the other windows. That sounds all quite nasty but for the time being it works :)
  • loxxy
    loxxy over 12 years
    Well from the compiler's point of view, your program without a window was nasty :p
  • em70
    em70 over 12 years
    Thanks, this sounds good, however I'm actually using the hidden window's dispatcher thread to create and show other windows. How would I do so in your example?
  • BSchlinker
    BSchlinker about 12 years
    Never knew about ShutdownMode.OnExplicitShutdown. Thanks for posting that -- alot of people who build tray applications in WPF are looking for something like that.