[C #] waits for the process to start to finish executing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.