c# - Process.Start takes long time to start outside application -


i have method launches second exe. issue i'm having if i'm in debug mode in visual studio , put breakpoint directly after process.start call second application launches if have no break points in vs or run main c# application outside of vs launching of second application via process.start can take 2 minutes. method below , put breakpoint see immediate launch of 2nd app @ line "if(null != _processmine)". put launch of second exe in worker thread because when close main exe want second exe close also.

    public static void runbtnprocessthread(string processname, string sargs, button btn)     {         // disable button until release newly launched process         btn.enabled = false;          backgroundworker worker = new backgroundworker();          worker.dowork += (doworksender, doworkargs) =>         {             processstartinfo startinfo = new processstartinfo();             startinfo.createnowindow = false;             startinfo.useshellexecute = false;             startinfo.filename = processname;             startinfo.arguments = sargs;             try             {                    using ( _processmine = process.start(startinfo))                 {                     if(null != _processmine)                         _processmine.waitforexit();                 }             }             catch (exception ex)             {                 string _funk = reflectionhelper.getmethodfullname(methodbase.getcurrentmethod());                  // error                 debug.assert(false, "error: " + ex.message);                  // log error.                 traceutil.logexception(_funk, ex);             }              system.threading.thread.sleep(500);         };          worker.runworkercompleted += (completedsender, completedargs) =>         {             btn.enabled = true;              _processmine)= null;         };                      worker.runworkerasync();     } 

you don't need separate thread scenario. can accomplish same thing subscribing process.exited() event:

    public static void runbtnprocessthread(string processname, string sargs, button btn)     {         // disable button until release newly launched process         btn.enabled = false;          processstartinfo startinfo = new processstartinfo();         startinfo.createnowindow = false;         startinfo.useshellexecute = false;         startinfo.filename = processname;         startinfo.arguments = sargs;          try         {             _processmine = process.start(startinfo);             _processmine.enableraisingevents = true;             _processmine.exited += (sender, e) =>             {                 btn.invoke((methodinvoker)delegate {                      btn.enabled = true;                  });                 _processmine = null;             };         }         catch (exception ex)         {             string _funk = reflectionhelper.getmethodfullname(methodbase.getcurrentmethod());              // error             debug.assert(false, "error: " + ex.message);              // log error.             traceutil.logexception(_funk, ex);         }     } 

you close using like:

    void form1_formclosing(object sender, formclosingeventargs e)     {         if (_processmine != null && !_processmine.hasexited)         {             // depending on type of app:             _processmine.closemainwindow();             // ... or ...             _processmine.kill();          }     } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -