Call other applications

Source: Internet
Author: User

Using System. Diagnostics;

Determine if the program is running

Code

/// <Summary>
/// Determine whether the program is running
/// </Summary>
/// <Param name = "prName"> process name </param>
/// <Returns> </returns>
Public static bool IsRunning (string prName)
{
Process [] procs = Process. GetProcesses ();
Foreach (Process pr in procs)
{
If (prName = pr. ProcessName)
{
Return true;
}
}
Return false;
}

 

Caller

Code
/// <Summary>
/// Start other applications
/// </Summary>
/// <Param name = "file"> Application Path: C: \ Program Files \ PrintNet T Designer 5.1 \ PNetTC.exe </param>
/// <Param name = "workdirectory"> application directory C: \ Program Files \ PrintNet T Designer 5.1 </param>
/// <Param name = "args"> command line parameters </param>
/// <Param name = "style"> window style </param>
Public bool StartProcess (string filePath, string programDir, string args, ProcessWindowStyle style)
{
Try
{

Process myProcess = new Process ();
ProcessStartInfo startInfo = new ProcessStartInfo (filePath, args );
MyProcess. StartInfo. CreateNoWindow = true;
MyProcess. StartInfo. UseShellExecute = false;
StartInfo. WindowStyle = style;
StartInfo. WorkingDirectory = programDir;
MyProcess. StartInfo = startInfo;
MyProcess. Start ();
// MyProcess. WaitForInputIdle ();
MyProcess. WaitForExit ();
Return true;
}
Catch (Exception e0)
{
FileDAL. WriteErrorLog (programDir, e0.Message );
MessageBox. Show ("An error occurred while starting the application! Cause: "+ e0.Message );
Return false;
}

}

Application

 

Code
String file = @ "C: \ Program Files \ PrintNet T Designer 5.1 \ PNetTC.exe ";
String filePaht = @ "C: \ Program Files \ PrintNet T Designer 5.1 ";

String parms = "E :\\ Shenfazhan \ modules \" + cardModule + "-difScriptDataInput1 ";
Parms = parms + "" + "E :\\ Shenfazhan \ data \" + dataName + "-e AdobePostScript3-c ";
Parms = parms + "" + "E :\\ Shenfazhan \ modules \ Shenzhen. job-f ";
Parms = parms + "" + "E :\\ Shenfazhan \ output \" + cardName + "Statistics. % e-o sta-dc Default-la ";
Parms = parms + "" + "E: \ Shenfazhan \ modules \ Local.txt ";

StartProcess (file, filePaht, parms, ProcessWindowStyle. Hidden)

 

This article demonstrates how to use the. NET FrameworkProcessClass to start another application from your code, and let the Code continue to run until the application ends.

When the code waits for the application to complete, there are two options:

  • Wait for another application to be completed or closed by the user indefinitely.
  • Specifies the timeout period. After this period, you can disable this application from the code.

This article provides two sample codes to demonstrate these two methods. In addition, the example of setting timeout takes into account the possibility that the other application stops responding (suspended) and can take necessary steps to close the application.

Back to Top Requirement

  • Microsoft C #. NET
Back to Top with namespace

You must importProcessClass namespace before you can run the following code example. Put the following line of code inNamespaceOrClassBefore declaration:

using System.Diagnostics;

Return to the top and wait for the Shell application to complete indefinitely

The following code starts another application (Notepad in this example) and waits for the application to close indefinitely.

    //How to Wait for a Shelled Process to Finish    //Get the path to the system folder.    string sysFolder= Environment.GetFolderPath(Environment.SpecialFolder.System);    //Create a new process info structure.    ProcessStartInfo pInfo = new ProcessStartInfo();    //Set the file name member of the process info structure.    pInfo.FileName = sysFolder + @"\eula.txt";    //Start the process.    Process p = Process.Start(pInfo);    //Wait for the window to finish loading.    p.WaitForInputIdle();    //Wait for the process to end.    p.WaitForExit();    MessageBox.Show("Code continuing...");

Return to the top and set timeout for the Shell application

The following code example sets a timeout for the Shell application. In this example, the timeout value is set to 5 seconds. You may want to adjust this number in milliseconds during the test ).

    //Set a time-out value.    int timeOut=5000;    //Get path to system folder.    string sysFolder=       Environment.GetFolderPath(Environment.SpecialFolder.System);    //Create a new process info structure.    ProcessStartInfo pInfo = new ProcessStartInfo();    //Set file name to open.    pInfo.FileName = sysFolder + @"\eula.txt";    //Start the process.    Process p = Process.Start(pInfo);    //Wait for window to finish loading.    p.WaitForInputIdle();    //Wait for the process to exit or time out.    p.WaitForExit(timeOut);    //Check to see if the process is still running.    if (p.HasExited == false)    //Process is still running.    //Test to see if the process is hung up.    if (p.Responding)          //Process was responding; close the main window.           p.CloseMainWindow();    else        //Process was not responding; force the process to close.   p.Kill();    MessageBox.Show("Code continuing...");

Back to Top troubleshooting

Sometimes it may be difficult to choose between the two methods. The main purpose of setting timeout is to prevent your application from being suspended due to another application. Timeout is more suitable for shell applications that execute background processing. At this time, users may not know that the application has been stopped or there is no convenient way to close it.

Http://support.microsoft.com/kb/305369/zh-cn

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.