See the light!

ASP.NET / VB.NET: running a long-duration process via an executable

Needed to run a long-duration report generation job from an ASP.NET page, ad-hoc, without blocking the page while it ran — the report has to run behind the scenes, with the user picking it up once it’s done.

System.Diagnostics.Process is the .NET class for this — it lets an ASP.NET page run an executable on the server in the background:

Dim myProcess As System.Diagnostics.Process = New System.Diagnostics.Process()
myProcess.StartInfo.WorkingDirectory = Me.Request.MapPath("~/bin")
myProcess.StartInfo.FileName = Me.Request.MapPath("~/bin/report.exe")
myProcess.StartInfo.Arguments = "1"
myProcess.Start()

report.exe here is the executable built to actually generate the report — it needs to properly release its own resources on completion, or you’ll run into memory issues down the line. .Arguments passes command-line arguments through — in this case, “1” as simply which report number to run, since report.exe is built to handle several.

Once .Start() fires, the process runs in the background. On completion, the report lands in a directory an ASP.NET page can list for the user to download from.

You can also enumerate currently running report processes:

Dim mySB As StringBuilder = New StringBuilder()
For Each myP As System.Diagnostics.Process In System.Diagnostics.Process.GetProcessesByName("report")
    mySB.Append(myP.ProcessName).Append(": process id: ").Append(myP.Id)
    mySB.Append(vbCrLf)
Next

From there you can do more with the retrieved process — stop it, or check how long it’s been running. This covers the fundamentals; the actual solution can be extended to whatever your specific case needs.