Thursday, November 9, 2006

Calling a Windows or DOS Process from a Web Application

Your favorite program (graphics package, spell checker, translator, data converter) is written in Windows or DOS and you like to utilize it in your new web application. Suppose you like to generate your favorite pie chart (in real-time) using a DOS application and display the resulting pie image on your web page. When the Web user presses the submit button you will generate a new chart and display it to the user. This could be done seamlessly by invoking your DOS application using the System.Diagnostics.Process.Start() method or the more versatile System.Diagnostics.ProcessStartInfo class.

Lets start with the basic System.Diagnostics.Process.Start() method
It is simply a method call with conventional DOS space-seperated parametrs. For example in C#:

System.Diagnostics.Process.Start("MyImageProgram.bat", @"MyFile -g -r MyLastParam");

The first parameter is the program you want to run and the second parameter is a space-seperated list of parameter(s) to pass to the program.
Process.Start() waits forever for the process to execute. This could make your web site non-responsive.
System.Diagnostics.ProcessStartInfo gives you much more control to accomplish the same thing. For example, you could control the maximum amount of time to wait for the process completion.
Lets look at an example:


using System.Diagnostics;
using System.IO;

public void Main()
{
SyncCall(@"C:\MyWebSites\Images\MyImageProgram.bat", @"MyFile -g -r MyLAstParam");
}

private void SyncCall(string strProgramName, string syncParam)
{


System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(strProgramName);

// store your parameters
psi.Arguments = syncParam;< /FONT >

psi.UseShellExecute = false;< /FONT > // Specify for proper execution

System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);< /FONT >

// optional for debugging. Grab the program console output
//System.IO.StreamReader myOutput = listFiles.StandardOutput;

// Wait a Maximum of 5000 milliseconds (5 seconds ) for the program execution.
listFiles.WaitForExit(5000);

// optional for debugging. Grab the program console output
/* string output;
if (listFiles.HasExited)
{
output = myOutput.ReadToEnd();
}
*/

}

Above, you specify the maximum amount to wait with the WaitForExit() method. If your program finishes faster than the amount of time you specified the system will not wait any longer and execution proceeds to the next line

(Also if your program outputs to the console you could grab the text using System.IO.StreamReader. Grabbing the text slows down execution. So it is only recommended for debugging.)

Above, after the image file is generated in real-time you could refer to it using an tag or server side tag.



Copyright @ Isaac Levy, Los Angeles, CA

2 comments:

Unknown said...

Any idea on how I can debug the app launched in this way, using visual studio ?

Anonymous said...

ITSolusenz departments manage all components web application, software development including, Application Development Company, software development company india, Software Development Services.