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

~OSD~ 2,176 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
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,278 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,031 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,713 questions
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 44,931 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 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.

    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,176 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.