c# - Stream Writer is null before program exits -


i trying add logfile program making, when tell main thread exit (using application.exitthread()), logstrmwriter null before ever gets there. simple script.

private static filestream applogstream; internal static string logfile; internal static streamwriter logstrmwriter; public static void main() {         logfile = application.startuppath + @"\archiver.log";     applogstream = new filestream(logfile, filemode.append, fileaccess.write, fileshare.read);     textwriter logtxtwriter = console.out;     streamwriter logstrmwriter = new streamwriter(applogstream);     if(!console) console.setout(logstrmwriter);      application.applicationexit += new eventhandler(onapplicationexit);     application.run(); } internal static void onapplicationexit(object sender, eventargs e) {     active = false; console.writeline("main thread shutting down. sending interrupt...");     archiver.stop(); console.writeline("shutdown. log , exit");     console.writeline();     logstrmwriter.flush();     logstrmwriter.close();     logstrmwriter.dispose(); } 

you're creating local variable hides static variable in question. such, never initialize logstrmwriter @ all.

textwriter logtxtwriter = console.out; /* streamwriter */ logstrmwriter = new streamwriter(applogstream);  // remove redeclaration here! if(!console) console.setout(logstrmwriter); 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -