none
WPF app crashes on startup

    Question

  • I've read tons of threads about this, but none helped figure it out. I have a WPF app, targeting .NET 3.5 and published using ClickOnce. The problem is that sometimes, *not always*, I see the installed app crash on startup. What I did notice is that when this happens once, it will happen again and again on that machine regardless if I do a clean install.

    Some posts suggested an odd approach - checking the solution out to a clean folder, opening just the project properties file and click publish using the right click context menu ... Oddly enough, this seemed to work at some point, but not any more. Something is up, and I need help finding it.

    I'm using the Single Instance Detection Sample approach to make sure i only have one instance of my app running (http://msdn.microsoft.com/en-us/library/ms771662.aspx) so my App.xaml.cs looks like:

    namespace DesktopApp
    {
      public class EntryPoint
      {
        [STAThread]
        public static void Main(string[] args)
        {
          SingleInstanceManager manager = new SingleInstanceManager();
          manager.Run(args);
        }
      }
    
      public class SingleInstanceManager : WindowsFormsApplicationBase
      {
        App app;
    
        public SingleInstanceManager()
        {
          this.IsSingleInstance = true;
        }
    
        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {
          // First time app is launched
          app = new App();
          app.InitializeComponent();
          app.Run();
          return false;
        }
    
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
          // Subsequent launches
          base.OnStartupNextInstance(eventArgs);
          app.Activate();
        }
      }
    
      /// <summary>
      /// Interaction logic for App.xaml
      /// </summary>
      public partial class App : Application
      {
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
          base.OnStartup(e);
        }
    
        public void Activate()
        {
          // Reactivate application's main window
          this.MainWindow.Activate();
        }
      }
    }
    

    I know the app crashes when it tries to do this:

    app = new App();
    

    I think I also saw this happen a few times when i run this in VS, and then I saw the breakpoint in App.Xaml saying something like "cannot locate resource App.xaml". Not sure if its the same problem but probably is.

    I have Debbugging Tools for Windows installed on a machine that I can reproduce this crash. I just need some help analyzing the dumps.

    Thanks in advance.

     

     

    Saturday, January 08, 2011 12:14 AM

Answers

  • As Brian says, make sure to catch all unhandled exceptions (I recommend by subscribed to Application.UnhandledException). Also, you definitely need to implement some sort of logging (I use log4net). If your application crashes at some clients, you can just ask them for the log files, or even better: prepare and e-mail with the log file attached so they only have to click the send button (or if you don't care about privacy, just send it immediately).
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for an MVVM framework for WPF and Silverlight? Check out Catel!
    Saturday, January 08, 2011 4:29 PM
  • hi,

     

    we are also using the single instance approach and get the same error, if our application doesn't shutdown gracefully.

    Be sure that no other instance of your application is running in background.

    Check the "Processes" list in the "Windows Task Manager".

     

    regards,

    genne

    Sunday, January 09, 2011 4:12 PM
  • So I took out all the VisualBasic stuff out and it seems to be working.

    Yikes, i thought that is the recommended way of doing a single instance wpf app :-( If this is not the case, what other options do I have?

    I'm surprised that there isn't a good, easy and reliable way of making sure you don't have 2 instances of the same wpf app running at the same time.

    Tuesday, January 11, 2011 8:47 PM
  • I completely agree with you.   WPF does not have a native way of creating a single instance application.  This is a major fail.  Unfortunately, if you need a single instance app, you must do this kind of work around.  Maybe you should google a little more to see if someone has come up with a better solution than the example given in the MSDN documentation.
    Tuesday, January 11, 2011 9:07 PM

All replies

  • When it crashes what error do you get?  I would recommend wrapping all the code in the code you provided with a Try/Catch and just show a messageBox of the error, that way when it does crash you will get an error message shown.
    Saturday, January 08, 2011 2:33 AM
  • As Brian says, make sure to catch all unhandled exceptions (I recommend by subscribed to Application.UnhandledException). Also, you definitely need to implement some sort of logging (I use log4net). If your application crashes at some clients, you can just ask them for the log files, or even better: prepare and e-mail with the log file attached so they only have to click the send button (or if you don't care about privacy, just send it immediately).
    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for an MVVM framework for WPF and Silverlight? Check out Catel!
    Saturday, January 08, 2011 4:29 PM
  • hi,

     

    we are also using the single instance approach and get the same error, if our application doesn't shutdown gracefully.

    Be sure that no other instance of your application is running in background.

    Check the "Processes" list in the "Windows Task Manager".

     

    regards,

    genne

    Sunday, January 09, 2011 4:12 PM
  • Sorry for the late response, and thank you all for the replies - I managed to narrow it down to the point of the crash. The error message, caught as an exception when wrapping the InitializeComponent() call is: "Cannot locate resource 'app.xaml' ".

    In the InitializeComponent() method, in the App.xaml.gs generated file, i see:

          System.Uri resourceLocater = new System.Uri("/<assembly_name>;component/app.xaml", System.UriKind.Relative);
          
          #line 1 "..\..\App.xaml"
          System.Windows.Application.LoadComponent(this, resourceLocater);
    

    and the LoadComponent is the guy failing to load app.xaml.

    I tried moving the InitializeComponent() call to the App constructor instead of in the OnStartUp (see code above) but same results.

    Now I'm stuck - why the heck isn't it locating App.xaml?

    App.xaml looks like:

    <Application x:Class="myNamespace.App"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      ShutdownMode="OnMainWindowClose"
      StartupUri="MainWindow.xaml">
      <Application.Resources>
        <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ResourceDictionary.xaml"></ResourceDictionary>
          </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
      </Application.Resources>
    </Application>
    
    Probably a silly thought, but could it be related to the fact that I changed the Assembly name in the Application tab of the settings page? Now the assembly name is different from the general namespace (myNamespace).
    Tuesday, January 11, 2011 5:31 PM
  • Genne, is it possible for you to look in your App.xaml.gs and see if you have those lines inside the InitializeComponent() method:

    System.Uri resourceLocater = new System.Uri("/<assembly_name>;component/app.xaml", System.UriKind.Relative);
       
       #line 1 "..\..\App.xaml"
       System.Windows.Application.LoadComponent(this, resourceLocater);
    
    

     

    I created a simple WPF test app, and all I see in that App.xaml.gs is:

          #line 4 "..\..\App.xaml"
          this.StartupUri = new System.Uri("Window6.xaml", System.UriKind.Relative);
    
    Not sure if this is expected or not, but trying to look at everthing here.
    Tuesday, January 11, 2011 5:50 PM
  • Did you change the build action for your app.xaml to Page?  I also believe the MainWindow may need to be Page as well.
    Tuesday, January 11, 2011 5:59 PM
  • Yes, both are set to Page.
    Tuesday, January 11, 2011 6:01 PM
  • how about deleting the app.xaml and setting the resources in code:

    ResourceDictionary rd = new ResourceDictionary();
    rd.Source = new Uri("/resource.xaml", UriKind.Relative);
    app.Resource = rd;
    
    
    
    Tuesday, January 11, 2011 6:07 PM
  • You mean leave app.xaml.cs, but delete the app.xaml file?
    Tuesday, January 11, 2011 6:10 PM
  • No I mean delete both. Then you code would like something like:

      public class EntryPoint
      {
        [STAThread]
        public static void Main(string[] args)
        {
          SingleInstanceManager manager = new SingleInstanceManager();
          manager.Run(args);
        }
      }
    
      public class SingleInstanceManager : WindowsFormsApplicationBase
      {
        SingleInstanceApplication app;
    
        public SingleInstanceManager()
        {
          this.IsSingleInstance = true;
        }
    
        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {
          // First time app is launched
          app = new SingleInstanceApplication();
          app.Run();
          return false;
        }
    
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
        {
          // Subsequent launches
          base.OnStartupNextInstance(eventArgs);
          app.Activate();
        }
      }
    
      public class SingleInstanceApplication : Application
      {
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
          base.OnStartup(e);
    
          // Create and show the application's main window
          MainWindow window = new MainWindow();
          window.Show();
        }
    
        public void Activate()
        {
          // Reactivate application's main window
          this.MainWindow.Activate();
        }
      }
    
    Or you could try just removing the resource dictionary from the app.xaml first and see if that is causing the issue.
    Tuesday, January 11, 2011 6:13 PM
  • So taking out the resource dictionary didn't make a difference. So i took out app.xaml entirely, and put the entry point code on top of the mainwindow.cs file, just above the mainwindow class.

    I first got a compile error when trying to call InitializeComponent(), and I am assuming it shouldn't be called since there is no app.xaml anymore, and hence no page or usercontrol to call it on - am I right? So I took the call out.

    Now I'm getting this exception:

    ************** Exception Text **************
    System.NullReferenceException: Object reference not set to an instance of an object.
       at <namespace>.SingleInstanceManager.OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnStartupNextInstanceMarshallingAdaptor(Object args)

    Oy ...

    Tuesday, January 11, 2011 8:12 PM
  • So I took out all the VisualBasic stuff out and it seems to be working.

    Yikes, i thought that is the recommended way of doing a single instance wpf app :-( If this is not the case, what other options do I have?

    I'm surprised that there isn't a good, easy and reliable way of making sure you don't have 2 instances of the same wpf app running at the same time.

    Tuesday, January 11, 2011 8:47 PM
  • I completely agree with you.   WPF does not have a native way of creating a single instance application.  This is a major fail.  Unfortunately, if you need a single instance app, you must do this kind of work around.  Maybe you should google a little more to see if someone has come up with a better solution than the example given in the MSDN documentation.
    Tuesday, January 11, 2011 9:07 PM
  • I appreciate your willingness to help Brian.

    BTW, I have come across a relatively new post releated to this topic:

    http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx

    I'm working on trying it out now.

    Tuesday, January 11, 2011 11:14 PM