Any application requires a user
intervention or an event to start its processing. For example, we can use
schedule task in windows or other scheduling softwares like controlm to initiate
the process based on some time and conditions. Sometimes, these scheduling
mechanisms will not be an ideal choice when we have a requirement to initiate
the process (exe) whenever required.
Well, this can be done by using Process
class in System. Diagnostics namespace in ASP.Net.
Moving forward, we will see how this
can be implemented using ASP.Net and C#.
Developing your exe
1. Since, you are considering using asp.net application to initiate an
exe there are chances where multiple users can initiate the exe at the same
time. Design your exe to handle this.
2. ASP.Net can just start your exe and will return to client. Develop
your exe so that it can automatically end by releasing all its
resources.
Steps
1. Open a new Asp.Net web application in your Visual Studio 2008.
2. To make the configurations easier, we will keep the exe location in
the appsettings in web.config so that it can be configured easily when moving
the application to different environments (from dev to prod).
<appSettings>
<add key="EXELOC"
value="F:\Articles\Working With Exe in
ASP.Net\Exe\DoComplexTask\DoComplexTask\bin\Debug\"/>
</appSettings>
Calling an exe from ASP.Net
To call an exe, we need to use the
Process class packed in System. Diagnostics namespace.
The below code can be used to call an
exe,
protected void btnSimpleStart_Click(object
sender, EventArgs e)
{
string locn =
ConfigurationManager.AppSettings["EXELOC"];
Process.Start(locn +
"DoComplexTask.exe");
}
Note
The service account in which the
ASP.Net is running should have proper access to exe location and should have all
the access to the resources the exe is trying to access; else the above code
will not start the exe.
At times, it is required to specify
additional settings to have a control on the exe running the server. The
ProcessStartInfo class in System. Diagnostics namespace can help us in these
scenarios to achieve our goal. Refer the code below,
protected void btnStart_Click(object
sender, EventArgs e)
{
string locn =
ConfigurationManager.AppSettings["EXELOC"];
Process myProcess = new
Process();
try
{
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName =
locn + "DoComplexTask.exe";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
The above is self explanatory, setting
UseShellExecute to false will suppress the use of OS shell to execute the
exe and setting CreateNoWindow to true will not open a window or cmd in
the server when the exe is initiated.
Calling an exe with parameters or
arguments
The exe can we are calling can take
inputs using command line arguments. Again, to call an exe with command line
arguments we can use the property called Arguments packed in ProcessStartInfo
class. Refer the code below,
protected void
btnStartArgs_Click(object sender, EventArgs e)
{
string locn =
ConfigurationManager.AppSettings["EXELOC"];
Process myProcess = new
Process();
try
{
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName =
locn + "DoComplexTask.exe";
myProcess.StartInfo.Arguments
= "Hello";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Calling an exe with multiple
parameters or arguments
We can provide multiple parameters by
separating the values with space and setting it in Arguments property. Refer
below,
protected void
btnStartArgs_Click(object sender, EventArgs e)
{
string locn =
ConfigurationManager.AppSettings["EXELOC"];
Process myProcess = new
Process();
try
{
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName =
locn + "DoComplexTask.exe";
myProcess.StartInfo.Arguments
= "100 false";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
Checking if an exe is already
running
The process class has some static
methods like GetCurrentProcess() and GetProcessesByName() which can be used to
get the current running process on the server. We can use these methods to find
if the exe we are interested is currently running. The below code uses
GetProcessesByName() method to find if an exe is already running in the
server.
protected void
btnIsexeRunning_Click(object sender, EventArgs e)
{
if
(Process.GetProcessesByName("DoComplexTask").Length > 0)
{
Response.Write("Exe already
running!!");
}
else
{
Response.Write("Exe is not
running!!");
}
}
|