c# - Prevent program from closing when console window closes -


so happening console program opens runs external c# program/code text file. runs fine when close original form window program/code executed closes. there way prevent text file code running closing?

this code calling run program

static void memexe(byte[] buffer)         {             assembly asm = assembly.load(buffer);              if (asm.entrypoint == null)                 throw new applicationexception("no entry point found!");              methodinfo epoint = asm.entrypoint;             epoint.invoke(null, null);         } 

where buffer code/program in bytes

and main

static void main(string[] args)         {             var data = file.readalltext(@"program.txt");             memexe(data);         } 

a workaround change project's output type in project's properties -> application -> output type console application windows application (see screenshot)

how change console application windows application

this way no console window created, the process neither appear 2 running processes nor can terminated closing console window.


this approach take. method executed in non-background-thread prevents process terminating once main-thread has terminated. however, still cannot close console window. that's why suggest switching windows application

using system; using system.io; using system.reflection; using system.threading;  namespace stackoverflow {     public static class program     {         static void main(string[] args)         {             runexternalfunctionthread t = new runexternalfunctionthread(file.readallbytes(@"program.txt"));             t.run();         }          private class runexternalfunctionthread         {             private byte[] code;              public runexternalfunctionthread(byte[] code)             {                 this.code = code;             }              public void run()             {                 thread t = new thread(new threadstart(this.runimpl));                  t.isbackground = false;                 t.priority = threadpriority.normal;                 t.setapartmentstate(apartmentstate.sta);                  t.start();             }              private void runimpl()             {                 assembly asm = assembly.load(this.code);                  if (asm.entrypoint == null) throw new applicationexception("no entry point found!");                  methodinfo epoint = asm.entrypoint;                 epoint.invoke(null, null);             }         }     } } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -