Share vb.net backgroundworker

myanhtrucker

New member
Har

** VB.NET Backgroundworker **

Lớp BackidentWorker trong VB.NET là một thành phần dựa trên nhóm chủ đề cho phép bạn thực hiện các tác vụ chạy dài không đồng bộ.Điều này có thể hữu ích cho các tác vụ không yêu cầu tương tác người dùng, chẳng hạn như tải xuống tệp hoặc xử lý một lượng lớn dữ liệu.

Để sử dụng lớp Backgroundworker, trước tiên bạn cần tạo một thể hiện của nó.Bạn có thể làm điều này bằng cách gọi từ khóa ** mới **:

`` `VBNet
Dim BW là người làm nền mới ()
`` `

Khi bạn đã tạo một thể hiện của lớp BackidentWorker, bạn có thể bắt đầu tác vụ bằng cách gọi phương thức ** RunWorkerAsync **.Phương thức này lấy một đại biểu làm tham số, trong đó chỉ định nhiệm vụ mà bạn muốn chạy không đồng bộ.Ví dụ: mã sau bắt đầu một tác vụ tải xuống tệp từ URL:

`` `VBNet
bw.runworkerasync (địa chỉ downloadfile)
`` `

Phương thức ** downloadfile ** là một đại biểu chỉ định nhiệm vụ mà bạn muốn chạy không đồng bộ.Phương thức này có một tham số duy nhất, đó là URL của tệp mà bạn muốn tải xuống.Phương thức ** downloadfile ** tải tệp vào một vị trí tạm thời trên máy tính của người dùng.

Khi hoàn tất nhiệm vụ, lớp Backgroundworker sẽ gọi ** Dowork ** Trình xử lý sự kiện.Trình xử lý sự kiện ** Dowork ** là nơi bạn có thể thực hiện công việc thực tế của nhiệm vụ.Ví dụ: mã sau đây cho thấy cách tải xuống một tệp trong bộ xử lý sự kiện ** Dowork **:

`` `VBNet
Private Sub bw_dowork (người gửi là đối tượng, e như doworkeventargs)
Dim URL dưới dạng chuỗi = E.Argument
Dim Stream As io.stream = io.file.openread (url)
Dim byte () như byte = new byte (stream.length - 1) {}
stream.read (byte, 0, byte.length)
stream.close ()

E.Result = byte
Kết thúc phụ
`` `

Trình xử lý sự kiện ** Dowork ** có hai tham số:

*** Người gửi ** là đối tượng đã nâng cao sự kiện.Trong trường hợp này, người gửi là đối tượng BackidentWorker.
*** E ** là một ví dụ của lớp ** DoworKEventArss **.Lớp ** DoworKeventArss ** chứa một thuộc tính duy nhất, ** Kết quả **, mà bạn có thể sử dụng để trả về kết quả của tác vụ.

Khi bộ xử lý sự kiện ** Dowork ** hoàn tất, lớp BackidentWorker sẽ gọi trình xử lý sự kiện ** ProgressChanged **.Trình xử lý sự kiện ** ProgressChanged ** là nơi bạn có thể báo cáo tiến trình của nhiệm vụ.Ví dụ: mã sau đây cho thấy cách báo cáo tiến trình tải xuống tệp:

`` `VBNet
Private Sub BW_ProTHE
'Hiển thị tiến trình tải xuống.
'...
Kết thúc phụ
`` `

Trình xử lý sự kiện ** ProgressChanged ** có hai tham số:

*** Người gửi ** là đối tượng đã nâng cao sự kiện.Trong trường hợp này, người gửi là đối tượng BackidentWorker.
*** E ** là một ví dụ của lớp ** ProgressChangedEventArgs **.Lớp ** ProgressChangedEventArss ** chứa hai thuộc tính:
*** Tiến trình ** là một giá trị từ 0 đến 1 cho biết tiến trình của tác vụ.
*** Userstate ** là một giá trị mà bạn có thể sử dụng để lưu trữ bất kỳ thông tin trạng thái nào bạn cần cho nhiệm vụ.

Khi nhiệm vụ hoàn tất, lớp Backgroundworker sẽ gọi ** RunWorkerCompleted ** Trình xử lý sự kiện.Trình xử lý sự kiện ** RunworkerCompleted ** là nơi bạn có thể làm sạch bất kỳ tài nguyên nào bạn sử dụng cho nhiệm vụ.Ví dụ: mã sau đây cho thấy cách dọn dẹp tệp tải xuống:

`` `VBNet
Private Sub bw_runworkerCompleted (người gửi là đối tượng, e như RunworkerCompletedEventArgs)
'Dọn dẹp tập tin tải xuống.
'...
Kết thúc phụ
`` `

Trình xử lý sự kiện ** RunWorkerCompleted ** lấy hai tham số:

*** Người gửi ** là đối tượng đã nâng cao sự kiện.Trong trường hợp này, người gửi là đối tượng BackidentWorker.
*** E ** là một ví dụ của
=======================================
#vb.net #backgroundworker #MultithReading #asynchronousprogramming #.net

**VB.NET BackgroundWorker**

The BackgroundWorker class in VB.NET is a thread pool-based component that allows you to perform long-running tasks asynchronously. This can be useful for tasks that do not require user interaction, such as downloading a file or processing a large amount of data.

To use the BackgroundWorker class, you first need to create an instance of it. You can do this by calling the **New** keyword:

```vbnet
Dim bw As New BackgroundWorker()
```

Once you have created an instance of the BackgroundWorker class, you can start the task by calling the **RunWorkerAsync** method. This method takes a delegate as a parameter, which specifies the task that you want to run asynchronously. For example, the following code starts a task that downloads a file from a URL:

```vbnet
bw.RunWorkerAsync(AddressOf DownloadFile)
```

The **DownloadFile** method is a delegate that specifies the task that you want to run asynchronously. This method takes a single parameter, which is the URL of the file that you want to download. The **DownloadFile** method downloads the file to a temporary location on the user's computer.

When the task is complete, the BackgroundWorker class will call the **DoWork** event handler. The **DoWork** event handler is where you can perform the actual work of the task. For example, the following code shows how to download a file in the **DoWork** event handler:

```vbnet
Private Sub bw_DoWork(sender As Object, e As DoWorkEventArgs)
Dim url As String = e.Argument
Dim stream As IO.Stream = IO.File.OpenRead(url)
Dim bytes() As Byte = New Byte(stream.Length - 1) {}
stream.Read(bytes, 0, bytes.Length)
stream.Close()

e.Result = bytes
End Sub
```

The **DoWork** event handler takes two parameters:

* **sender** is the object that raised the event. In this case, the sender is the BackgroundWorker object.
* **e** is an instance of the **DoWorkEventArgs** class. The **DoWorkEventArgs** class contains a single property, **Result**, which you can use to return the results of the task.

When the **DoWork** event handler is complete, the BackgroundWorker class will call the **ProgressChanged** event handler. The **ProgressChanged** event handler is where you can report the progress of the task. For example, the following code shows how to report the progress of a file download:

```vbnet
Private Sub bw_ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
' Display the progress of the download.
' ...
End Sub
```

The **ProgressChanged** event handler takes two parameters:

* **sender** is the object that raised the event. In this case, the sender is the BackgroundWorker object.
* **e** is an instance of the **ProgressChangedEventArgs** class. The **ProgressChangedEventArgs** class contains two properties:
* **Progress** is a value from 0 to 1 that indicates the progress of the task.
* **UserState** is a value that you can use to store any state information that you need for the task.

When the task is complete, the BackgroundWorker class will call the **RunWorkerCompleted** event handler. The **RunWorkerCompleted** event handler is where you can clean up any resources that you used for the task. For example, the following code shows how to clean up a file download:

```vbnet
Private Sub bw_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
' Clean up the file download.
' ...
End Sub
```

The **RunWorkerCompleted** event handler takes two parameters:

* **sender** is the object that raised the event. In this case, the sender is the BackgroundWorker object.
* **e** is an instance of
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top