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).
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.
Run it in command line mode in a debugger to see if an exception is being thrown, or put a try..catch around the code in your OnStartup routine.
If an exception goes unhandled in any thread of a process, the entire process is terminated abruptly.
Also make sure you are closing any files or streams that you are writing the log to. Most file I/O is buffered these days, and if you don't close the stream correctly the last buffer's worth of data may not be committed to the output device.
精彩评论