You can also use a timeout when waiting for a process to exit. Sorry about using an image, For some reason the site will not accept code. Q&A strikes again!
VB.Net Terminate the running process after a certain time.
Hi,
Running a VB forms application and would like to know if it's possible to terminate/exit the VB application if the process is still running after a certain time.
For example, I would like to terminate this process, PSProcess if it has not been finished after 3 minutes. After 3 minutes, terminate this process and close the VB application and reboot the PC.
Dim PSProcess As Process = New Process()
PSProcess.StartInfo.FileName = "PowerShell.exe"
PSProcess.StartInfo.Arguments = "-ExecutionPolicy Bypass -File C:\Windows\myCustomeScript.ps1"
PSProcess.StartInfo.UseShellExecute = False
PSProcess.StartInfo.CreateNoWindow = True
PSProcess.StartInfo.RedirectStandardError = True
PSProcess.Start()
PSProcess.WaitForExit()
Dim sStdErr_PSProcess As String = PSProcess.StandardError.ReadToEnd()
Console.WriteLine("Exit code : {0}", PSProcess.ExitCode)
Console.WriteLine("StdErr : {0}", sStdErr_PSProcess)
2 additional answers
Sort by: Most helpful
-
Jiachen Li-MSFT 30,931 Reputation points Microsoft Vendor
2024-03-05T02:31:28.8333333+00:00 Hi @~OSD~ ,
You can configure a
Timer
after starting the process and let the timer detect if the process has exited after three minutes.Best Regards.
Jiachen Li
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
-
~OSD~ 2,176 Reputation points
2024-03-05T21:45:13.85+00:00 Thank you both, it seems to be working.