VB.Net Terminate the running process after a certain time.

~OSD~ 2,201 Reputation points
2024-03-04T23:07:02.4433333+00:00

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)
Windows for business Windows Client for IT Pros User experience Other
Developer technologies VB
Developer technologies Visual Studio Other
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 49,461 Reputation points
    2024-03-05T11:02:59.92+00:00

    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!

    enter image description here

    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    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.

    User's image

    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.

    1 person found this answer helpful.
    0 comments No comments

  2. ~OSD~ 2,201 Reputation points
    2024-03-05T21:45:13.85+00:00

    Thank you both, it seems to be working.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.