Sometimes we start a process, we have to wait until this process is done, or, for a while,
Close the process and then continue to go down.
Example
Sample1
Wait until the application finishes executing
Wait for the application to complete private void Btnprocessindefinitely_click (object sender, EventArgs e) { //config file case path string target = System.IO.Path.Combine (application.startuppath,@ "Test.txt"); Get the complete absolute path target = System.IO.Path.GetFullPath (target); Start process p = process.start (target); Let the process component wait for the related processes to enter idle state. P.waitforinputidle (); Sets the time to wait for the associated process to end, and prevents the current thread from executing until the waiting time is exhausted or the process has ended. p.waitforexit (); if (P! = null) { p.close (); P.dispose (); p = null; } This. Close (); }
Sample2
Wait for application (7 seconds)
Wait for application (7 seconds) private void Btnwaitprocessfor7_click (object sender, EventArgs e) {//config file case Path St Ring target = System.IO.Path.Combine (Application.startuppath, @ "Test.txt"); Get the complete absolute path target = System.IO.Path.GetFullPath (target); Start process P = Process.Start (target); Let the process component wait for the related processes to enter idle state. P.waitforinputidle (); Set the time to wait for the relevant process to end, set this way for 7 seconds. p.WaitForExit (7000); Value if the application is closed within a specified time. HasExited is true. If you wait until the specified time has not closed the program, this time value. HasExited is false, enter the judgment if (!p.hasexited) {///test if the process still has a response if (p.responding) { Closes the user interface process P.closemainwindow (); } else {//stops the related process immediately. That is, the process does not respond, forcing the shutdown of P.kill (); }} if (P! = null) {p.close (); P.dispose (); p = NUll } this. Close (); }
Sample3
Waiting for Applications with multithreading (7 seconds)
The above two methods, when waiting for the process to complete, the form screen will lock, cannot redraw, this side provides an improved method,
If there are other methods, look at the guidance of predecessors.
Use multithreaded wait application (7 seconds) private void Btnmultithreadwaitprocess_click (object sender, EventArgs e) {//Build thread Object Thread thread = new Thread (new ThreadStart (startprocess)); Start thread threads. Start (); Wait for the thread to finish processing while (thread. ThreadState = = System.Threading.ThreadState.Running | | Thread. ThreadState = = System.Threading.ThreadState.WaitSleepJoin) {application.doevents (); } this. Close (); } private void StartProcess () {//config file case path string target = System.IO.Path.Combine ( Application.startuppath, @ "Test.txt"); Get the complete absolute path target = System.IO.Path.GetFullPath (target); Start process P = Process.Start (target); Let the process component wait for the related processes to enter idle state. P.waitforinputidle (); Set the time to wait for the relevant process to end, set this way for 7 seconds. p.WaitForExit (7000); If the application is closed within a specified time, ValUe. HasExited is true. If you wait until the specified time has not closed the program, this time value. HasExited is false, enter the judgment if (!p.hasexited) {///test if the process still has a response if (p.responding) { Closes the user interface process P.closemainwindow (); } else {//stops the related process immediately. That is, the process does not respond, forcing the shutdown of P.kill (); }} if (P! = null) {p.close (); P.dispose (); p = null; } }
Source
Testprocesswaitting.rar
[C #] waits for the process to start to finish executing