Search code examples
c#command-line-tool

Program closing before it's really finished


I have a program with two modes: GUI & Just Run. GUI loads a WPF form and lets me adjust settings, while Just Run loads said settings and just... well... runs.

It's a glorified, customizable tool to batch-move files.

The problem: The "log" doesn't finish. It seems like it's still writing while the program closes, thus cutting off the end of the log.

Main App body:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        if (!e.Args.Contains("-gui"))
        {
            Process.SettingsPopulate();
            Process.WriteLogHead();
            Process.MoveFiles();
            Process.WriteLogEnd();
        }
        else
        {
            new MainWindow().ShowDialog();
        }
        this.Shutdown();
    }
}

Real simple. If command-line has -gui, it loads WPF... else it just runs.

    public static void WriteLogEnd()
    {
        foreach (var log in from i in Items
                            where i.Used == false
                            let build = new StringBuilder()
                            select build.AppendFormat("{0, -12} > {1} > Not Found", _time, i.FileName))
        {
            Logs(log);
        }

        Logs("");
        Logs("Fin.");
    }

Logs is a simple "Write to Console, Write to file" method. I iterate thru my list of processed files, finding items that aren't used... aka "not found". Before it ends the list, and write Fin, it closes.

Has me perplexed. How can I stop the file from closing before WriteLogEnd finishes (Since, afaik, it shouldn't be able to end before then anyways).


Solution

  • Logs is a simple "Write to Console, Write to file" method.

    If it writes to File, add a Logs.Close() or something. You have to close a FileStream to flush its cache.