1

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

  • loads
  • pulls xml location from app.config
  • loads parm class from XmlDoc
  • Moves Files successfully
  • crashes with NullReferenceException was Unhandled error. I can comment out everything and it erros on "finish" of OnStart
WernerCD
  • 2,137
  • 6
  • 31
  • 51

2 Answers2

1

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • I've removed the background worker (Since I'm working on the automatic process more than the gui atm). Same issue. – WernerCD Nov 23 '10 at 17:10
  • Not exactly the "answer" but it did make me think about pulling the background worker (and other junk) until I figured out the right line I needed to pull. – WernerCD Nov 24 '10 at 00:39
0

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).

Community
  • 1
  • 1
WernerCD
  • 2,137
  • 6
  • 31
  • 51