Share Hướng dẫn sử dụng BackgroundWorker trong VB.NET

..

## Hướng dẫn sử dụng BackidentWorker trong VB.Net

Điều khiển BackgroundWorker là điều khiển Windows Forms cho phép bạn thực hiện các tác vụ chạy dài trong nền mà không chặn luồng UI.Điều này có thể hữu ích cho các tác vụ như tải xuống tệp, xử lý hình ảnh hoặc gửi email.

Để sử dụng điều khiển BackgroundWorker, trước tiên bạn cần thêm nó vào biểu mẫu của mình.Bạn có thể làm điều này bằng cách kéo và thả điều khiển từ hộp công cụ vào biểu mẫu của bạn.

Khi bạn đã thêm điều khiển BackgroundWorker vào biểu mẫu của mình, bạn cần đặt các thuộc tính của nó.Tài sản quan trọng nhất là tài sản của WorkerReportSproTHER.Thuộc tính này xác định xem điều khiển BackgroundWorker sẽ gửi các bản cập nhật tiến trình đến luồng UI.

Bạn cũng có thể đặt thuộc tính Công nhân của Bộ điều khiển BackidentWorker.Thuộc tính này xác định liệu người dùng có thể bị hủy có thể bị hủy bởi người dùng hay không.

Khi bạn đã đặt các thuộc tính của Bộ điều khiển BackgroundWorker, bạn có thể bắt đầu bằng cách gọi phương thức StartWorkerAsync của nó.Phương pháp này sẽ bắt đầu luồng của Bộ điều khiển BackgroundWorker và bắt đầu nhiệm vụ chạy dài.

Trình xử lý sự kiện Dowork của Bộ điều khiển BackidentWorker được gọi là khi nhiệm vụ chạy dài bắt đầu.Trình xử lý sự kiện này là nơi bạn nên viết mã thực hiện nhiệm vụ chạy dài.

Trình xử lý sự kiện ProgressChanged của Bộ điều khiển Backgodworker được gọi khi điều khiển BackgroundWorker gửi bản cập nhật tiến trình đến luồng UI.Trình xử lý sự kiện này là nơi bạn nên cập nhật giao diện người dùng để phản ánh tiến trình của nhiệm vụ chạy dài.

Trình xử lý sự kiện RunworkerCompleted của Bộ điều khiển BackidentWorker được gọi là khi nhiệm vụ chạy dài kết thúc.Trình xử lý sự kiện này là nơi bạn nên dọn dẹp bất kỳ tài nguyên nào được sử dụng bởi nhiệm vụ chạy dài.

Dưới đây là một ví dụ về cách sử dụng điều khiển Backgoundworker trong vb.net:

`` `VBNet
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

'Tạo một điều khiển Backgroundworker mới.
Dim Backgroundworker làm nền mới ()

'Đặt các thuộc tính của Bộ điều khiển BackidentWorker.
Backgroundworker.workerReportSproTHER = true
Backgroundworker.workersupportScancellation = true

'Bắt đầu chủ đề của Bộ điều khiển Backgroundworker.
Backgroundworker.startworkerasync ()

Kết thúc phụ

Sub Backgroundworker_Dowork (người gửi là đối tượng, e như doworkeventargs) xử lý backedworker.dowork

'Thực hiện nhiệm vụ chạy dài.
'Trong ví dụ này, chúng tôi sẽ tải xuống một tệp từ Internet.

Dim WebClient là WebClient mới ()
Dim FileBytes As Byte () = WebClient.doLoadBytes ("https://www.example.com/file.txt")

'Lưu tệp vào đĩa.
Dim FileStream dưới dạng FileStream mới ("C: \ temp \ file.txt", filemode.create)
fileStream.write (fileBytes, 0, fileBytes.length)
filestream.close ()

Kết thúc phụ

Sub Backgroundworker_ProTHERChanged (người gửi là đối tượng, e như ProgressChangedEventArss) xử lý Backgroundworker.ProTHERChanged

'Cập nhật giao diện người dùng để phản ánh tiến trình của nhiệm vụ chạy dài.

Me.ProTHERBar1.value = E.ProTHERPercentage

Kết thúc phụ

Sub Backgroundworker_RunworkerCompleted (Người gửi là đối tượng, E AS RunworkerCompletedEventArss) xử lý Backgroundworker.RunWorkerCompleted

'Nhiệm vụ chạy dài đã kết thúc.

'Làm sạch bất kỳ tài nguyên nào được sử dụng bởi nhiệm vụ chạy dài.

Kết thúc phụ
`` `

## Tài nguyên bổ sung

* [Lớp học Backgroundworker] (https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.backgroundworker)
* [Hướng dẫn về mặt tường] (https://docs.microsoft.com/en-us/do...ms/controls/backgroundworker-control-tutorial)
* [MultiThreading trong vb.net] (https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/multithreading-in-vb-net)
* [Lập trình không đồng bộ trong vb.net] (https://docs.microsoft.com/en-us/dot
=======================================
#backgroundworker #vb.net #MultithReading #asynchronous Programming #Concurrency

## Instructions for using BackgroundWorker in VB.NET

The BackgroundWorker control is a Windows Forms control that allows you to perform long-running tasks in the background without blocking the UI thread. This can be useful for tasks such as downloading files, processing images, or sending emails.

To use the BackgroundWorker control, you first need to add it to your form. You can do this by dragging and dropping the control from the Toolbox onto your form.

Once you have added the BackgroundWorker control to your form, you need to set its properties. The most important property is the WorkerReportsProgress property. This property determines whether the BackgroundWorker control will send progress updates to the UI thread.

You can also set the BackgroundWorker control's WorkerSupportsCancellation property. This property determines whether the BackgroundWorker control can be canceled by the user.

Once you have set the BackgroundWorker control's properties, you can start it by calling its StartWorkerAsync method. This method will start the BackgroundWorker control's thread and begin the long-running task.

The BackgroundWorker control's DoWork event handler is called when the long-running task begins. This event handler is where you should write the code that performs the long-running task.

The BackgroundWorker control's ProgressChanged event handler is called when the BackgroundWorker control sends a progress update to the UI thread. This event handler is where you should update the UI to reflect the progress of the long-running task.

The BackgroundWorker control's RunWorkerCompleted event handler is called when the long-running task has finished. This event handler is where you should clean up any resources that were used by the long-running task.

Here is an example of how to use the BackgroundWorker control in VB.NET:

```vbnet
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

' Create a new BackgroundWorker control.
Dim backgroundWorker As New BackgroundWorker()

' Set the BackgroundWorker control's properties.
backgroundWorker.WorkerReportsProgress = True
backgroundWorker.WorkerSupportsCancellation = True

' Start the BackgroundWorker control's thread.
backgroundWorker.StartWorkerAsync()

End Sub

Private Sub BackgroundWorker_DoWork(sender As Object, e As DoWorkEventArgs) Handles backgroundWorker.DoWork

' Perform the long-running task.
' In this example, we will download a file from the internet.

Dim webClient As New WebClient()
Dim fileBytes As Byte() = webClient.DownloadBytes("https://www.example.com/file.txt")

' Save the file to disk.
Dim fileStream As New FileStream("C:\Temp\file.txt", FileMode.Create)
fileStream.Write(fileBytes, 0, fileBytes.Length)
fileStream.Close()

End Sub

Private Sub BackgroundWorker_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles backgroundWorker.ProgressChanged

' Update the UI to reflect the progress of the long-running task.

Me.ProgressBar1.Value = e.ProgressPercentage

End Sub

Private Sub BackgroundWorker_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles backgroundWorker.RunWorkerCompleted

' The long-running task has finished.

' Clean up any resources that were used by the long-running task.

End Sub
```

## Additional Resources

* [BackgroundWorker Class](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.backgroundworker)
* [BackgroundWorker Tutorial](https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/backgroundworker-control-tutorial)
* [Multithreading in VB.NET](https://docs.microsoft.com/en-us/dotnet/framework/winforms/advanced/multithreading-in-vb-net)
* [Asynchronous Programming in VB.NET](https://docs.microsoft.com/en-us/dot
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top