Share vb.net kill process by id

lykimestrella

New member
### Cách giết một quy trình bằng ID trong VB.Net

**Tổng quan**

Trong hướng dẫn này, bạn sẽ học cách tiêu diệt một quy trình bằng ID của nó trong VB.NET.Bạn sẽ tìm hiểu cách sử dụng phương thức `process.kill ()`, cũng như cách xử lý sự kiện `ctrl+c` để chấm dứt một cách duyên dáng một quá trình.

** Điều kiện tiên quyết **

Để làm theo với hướng dẫn này, bạn sẽ cần những điều sau đây:

* Visual Studio 2019 trở lên
* Máy tính Windows

** `process.kill ()` Phương thức **

Phương thức `process.kill ()` được sử dụng để chấm dứt một quá trình bằng ID của nó.Mã sau đây cho thấy cách sử dụng phương thức `process.kill ()`:

`` `VBNet
Dim ProcessID as Integer = Process.getProcessId ("myProcess.exe")
Process.Kill (ProcessID)
`` `

Phương thức `process.getProcessId ()` Trả về ID của quy trình với tên được chỉ định.Phương thức `process.kill ()` lấy ID quy trình làm đối số duy nhất của nó.

** Xử lý sự kiện `ctrl+c` **

Khi bạn nhấn phím `ctrl+c`, sự kiện` ctrl+c` được nâng lên.Bạn có thể xử lý sự kiện này để chấm dứt một cách duyên dáng một quá trình.Mã sau đây cho thấy cách xử lý sự kiện `ctrl+c`:

`` `VBNet
Sub Sub Form1_Keydown (Người gửi là đối tượng, E như KeyEventArss) xử lý tôi.Keydown
Nếu e.keydata = keys.control andalso e.keycode = keys.c thì
'Chấm dứt quá trình.
Process.kill (process.getCurrentProcess (). Id)
Kết thúc nếu
Kết thúc phụ
`` `

**Ví dụ**

Mã sau đây cho thấy một ví dụ đầy đủ về cách tiêu diệt một quy trình bằng ID của nó trong VB.NET:

`` `VBNet
Lớp công khai Mẫu1

Công khai Sub mới ()
Khởi tạo ()
Kết thúc phụ

Nút phụ riêng1_click (người gửi dưới dạng đối tượng, e là EventArgs) Nút tay cầm1.Click
'Nhận ID quy trình của quy trình "myProcess.exe".
Dim ProcessID as Integer = Process.getProcessId ("myProcess.exe")

'Giết quá trình.
Process.Kill (ProcessID)
Kết thúc phụ

Sub Sub Form1_Keydown (Người gửi là đối tượng, E như KeyEventArss) xử lý tôi.Keydown
Nếu e.keydata = keys.control andalso e.keycode = keys.c thì
'Chấm dứt quá trình.
Process.kill (process.getCurrentProcess (). Id)
Kết thúc nếu
Kết thúc phụ

Kết thúc lớp học
`` `

** hashtags **

* #vb.net
* #quá trình
* #Giết
* #Nhận dạng
* #terminate
=======================================
### How to Kill a Process by ID in VB.NET

**Overview**

In this tutorial, you will learn how to kill a process by its ID in VB.NET. You will learn how to use the `Process.Kill()` method, as well as how to handle the `Ctrl+C` event to gracefully terminate a process.

**Prerequisites**

To follow along with this tutorial, you will need the following:

* Visual Studio 2019 or later
* A Windows computer

**The `Process.Kill()` Method**

The `Process.Kill()` method is used to terminate a process by its ID. The following code shows how to use the `Process.Kill()` method:

```vbnet
Dim processID As Integer = Process.GetProcessId("MyProcess.exe")
Process.Kill(processID)
```

The `Process.GetProcessId()` method returns the ID of the process with the specified name. The `Process.Kill()` method takes the process ID as its only argument.

**Handling the `Ctrl+C` Event**

When you press the `Ctrl+C` key, the `Ctrl+C` event is raised. You can handle this event to gracefully terminate a process. The following code shows how to handle the `Ctrl+C` event:

```vbnet
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyData = Keys.Control AndAlso e.KeyCode = Keys.C Then
' Terminate the process.
Process.Kill(Process.GetCurrentProcess().Id)
End If
End Sub
```

**Example**

The following code shows a complete example of how to kill a process by its ID in VB.NET:

```vbnet
Public Class Form1

Public Sub New()
InitializeComponent()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Get the process ID of the "MyProcess.exe" process.
Dim processID As Integer = Process.GetProcessId("MyProcess.exe")

' Kill the process.
Process.Kill(processID)
End Sub

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyData = Keys.Control AndAlso e.KeyCode = Keys.C Then
' Terminate the process.
Process.Kill(Process.GetCurrentProcess().Id)
End If
End Sub

End Class
```

**Hashtags**

* #vb.net
* #Process
* #Kill
* #id
* #terminate
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top