How To Close A Program From Visual Basic

Table of contents:

How To Close A Program From Visual Basic
How To Close A Program From Visual Basic

Video: How To Close A Program From Visual Basic

Video: How To Close A Program From Visual Basic
Video: Visual Basic.net: How to exit Message Application 2024, May
Anonim

The flexible and powerful programming language Visual Basic. NET, being one of the specially created tools for developing applications for the Microsoft. NET platform, allows you to fully use all its capabilities. In particular, the components of the System. Diagnostics namespace allow you to interact with processes, event logs, and performance counters. For example, you can close a program from Visual Basic using the Process class.

How to close a program from Visual basic
How to close a program from Visual basic

Necessary

Microsoft Visual Studio

Instructions

Step 1

Import the System, System. Diagnostics, and System. Threading namespaces. Add the following lines of code to the beginning of the module:

Imports System

Imports System. Diagnostics

Imports System. Threading

This is just for the convenience of using the components associated with these namespaces.

Step 2

Get the data of the process to be closed. Use the System. Diagnostics. Process class object. Declare a variable of this class:

Dim oProc As Process

Then use some method to find the required process.

Step 3

If the program, which will need to be closed in the future, is launched by the application being developed, then simply save the object returned by the Start method at startup:

oProc = Process. Start ("app.exe")

Step 4

If you need to close a process with a known identifier, use the static GetProcessById method of the Process class to get the corresponding object:

oProc = Process. GetProcessById (nID)

Here nID is the numeric identifier of the process.

Step 5

If only some of the characteristics of the target process are known, search for it. Get the list of processes running on the local machine as an array of objects of the Process class. Use the GetProcesses (returns all processes) or GetProcessesByName (only processes with the given name) methods:

Dim aoAllProcesses As Process () = Process. GetProcesses ()

Dim aoProcsByName As Process () = Process. GetProcessesByName ("app.exe")

List the array objects using a loop:

Dim oProc As Process

For Each oProc In aoAllProcesses

'actions on oProc

Next

Loop through the properties MainModule, MainWindowTitle, ProcessName, etc. to find the desired object.

Step 6

Try to terminate the program by sending a close message to its main window. Call the CloseMainWindow method of the object corresponding to the target process. If necessary, wait for the application to complete by calling WaitForExit, for example:

oProc. CloseMainWindow ()

oProc. WaitForExit ()

This method does not guarantee the termination of the program, as the window close message is often processed and can be ignored.

Step 7

Wait for a short amount of time after calling CloseMainWindow to ensure that the program terminates. Use the Sleep method of the Thread class. Then check the status of the process by examining the HasExited property and, if it hasn't finished, call the Kill method:

Thread. Sleep (6000)

oProc. Refresh ()

If not oProc. HasExited Then

oProc. Kill ()

End If

If desired, you can poll the status of the process in a loop, issuing periodic prompts to the user about the need to terminate the application without saving data. And only if you agree to call Kill.

Step 8

Free up system resources after the program finishes using the Close method:

oProc. Close ()

Step 9

To avoid unexpected errors during application execution, place the entire program closing algorithm in a Try-Catch-End Try block. Implement full-fledged exception handling with diagnostic messages, if necessary.

Recommended: