WernerCD
WernerCD

Reputation: 2157

C# WPF App crashes at the end

I have a simple program. Its job is to move files from A to B (a glorified bat file honestly).

The problem I'm having is that it crashes... at the end.

App.xaml.cs:

<Application x:Class="app.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:properties="clr-namespace:app.Properties" 
             StartupUri="Views\MainWindow.xaml">
    <Application.Resources>
        <properties:Settings x:Key="Settings" />
    </Application.Resources>
</Application>

App.CS:

using System.ComponentModel;
using System.Linq;
using System.Windows;
using app.Model;
using app.Properties;
using app.Views;

namespace app
{
    public partial class App
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            string xmlDoc = Settings.Default.Config;
            var parms = new MoveFilesParams(xmlDoc);
            Process.MoveFiles(parms);
        }
    }
}

Simple body. Assuming I pass it no command line parameter, it's supposed to just run automatically. With parameters, it will run the WPF.

It pulls settings from the xmlDoc populated in the Application settings. It then passes those settings via Parms class. Then it either runs, or popu

When run, I've tried a try/catch without errors inside the OnStartup - and it errors out after the OnStarup block with a NullReferenceException.

Edit 1: Sorry if I'm not responding fast enough (Yay for impatient people downvoting because they are impatient), but I've removed the background worker parts since I'm not using that right now. I'm still getting the same Null error at the end. Updated code here to reflect removal of BackgroundWorker.

Edit 2: Removed the GUI aspect of App.cs (since background worker and gui is secondary atm). So this program, as of right now, simply

Upvotes: 1

Views: 405

Answers (2)

WernerCD
WernerCD

Reputation: 2157

WPF Command Line

I had used this question (or one very similar) to setup my WPF Command-line. The problem must have appeared when I rebuilt the application and didn't remove

StartupUri="Views\MainWindow.xaml"

Which meant that after the process ran, it would try to access an uninitialized object giving a null-exception error.

At least I'm sure that's what the issue is. Removing StartupUri removes that problem (Although now I have another error that I might post a new question about).

Upvotes: 0

Jon
Jon

Reputation: 437704

If your main thread exits before the background task is completed, and if BackgroundWorker creates a background thread (I think it does, but not 100% sure), then when your UI thread exits the background thread would be shut down aggressively.

Try to keep your main thread running until you are definitely sure that the BackgroundWorker has completed, and see if it still crashes. If it does not, then you found the problem.

Upvotes: 1

Related Questions