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

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -