Answered by:
WPF app crashes on startup

-
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.
Question
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!- Marked as answer by Yves.ZModerator Monday, January 17, 2011 6:41 AM
-
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
- Marked as answer by Yves.ZModerator Monday, January 17, 2011 6:41 AM
-
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.
- Marked as answer by Yves.ZModerator Monday, January 17, 2011 6:41 AM
-
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.
- Marked as answer by Yves.ZModerator Monday, January 17, 2011 6:42 AM
All replies
-
-
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!- Marked as answer by Yves.ZModerator Monday, January 17, 2011 6:41 AM
-
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
- Marked as answer by Yves.ZModerator Monday, January 17, 2011 6:41 AM
-
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:
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).<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>
-
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:
Not sure if this is expected or not, but trying to look at everthing here.#line 4 "..\..\App.xaml" this.StartupUri = new System.Uri("Window6.xaml", System.UriKind.Relative);
-
-
-
-
-
No I mean delete both. Then you code would like something like:
Or you could try just removing the resource dictionary from the app.xaml first and see if that is causing the issue.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(); } }
-
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 ...
-
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.
- Marked as answer by Yves.ZModerator Monday, January 17, 2011 6:41 AM
-
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.
- Marked as answer by Yves.ZModerator Monday, January 17, 2011 6:42 AM
-
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.