c# - Making thread wait for exit without resorting to Thread.Sleep() -
i'm having problem trying wrap head around i'm doing wrong while attempting simple threading operation in 1 of applications.
here's i'm after: want main thread spawn separate thread; separate thread open program, feed program argument (a filename) , once program closes, child thread terminate , main thread can continue it's work. i've created simple code example illustrate. , truely, doesn't have separate thread, needs wait until program done it's work. doing wrong here? appreciated!
class program { static void main(string[] args) { console.writeline("opening...."); var t = new thread(startprogram); t.start(); t.join(); console.writeline("closed"); console.readline(); } private static void startprogram() { var startinfo = new processstartinfo(); startinfo.filename = @"c:\program.exe"; startinfo.arguments = @"file.txt"; var p = process.start(startinfo); p.waitforexit(); } }
why bother launching separate thread? current main method can rewritten more as:
static void main(string[] args) { console.writeline("opening...."); startprogram(); console.writeline("closed"); console.readline(); }
startprogram
waiting process exit... why introduce new thread? perhaps i'm missing something...
Comments
Post a Comment