Share Học cách sử dụng BackgroundWorker trong C#

uyenthytranvy

New member
#C ##backgroundworker #MultithReading #asynchronous #Programming ### Tìm hiểu cách sử dụng BackidentWorker trong C #

Lớp 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 trong khi giao diện người dùng của bạn vẫn phản hồi.Điều này có thể hữu ích cho các tác vụ như tải xuống các tệp, tải lên các 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ó trong mã của bạn.Bạn có thể làm điều này bằng cách gọi hàm xây dựng `boardnessworker ()`.

`` `C#
Công nhân Backgroundworker = new BackidentWorker ();
`` `

Khi bạn đã tạo một thể hiện của lớp BackidentWorker, bạn có thể bắt đầu một tác vụ mới bằng cách gọi phương thức `RunWorkerAsync ()`.Phương pháp này lấy một đại biểu làm tham số của nó.Đại biểu chỉ định nhiệm vụ mà bạn muốn thực hiện trong nền.

`` `C#
công nhân.runworkerasync (Dowork);
`` `

`Dowork ()` Đại biểu là một phương thức mà bạn phải xác định.Phương pháp này chịu trách nhiệm thực hiện nhiệm vụ chạy dài.Phương thức `Dowork ()` được gọi trên một luồng riêng từ luồng chính.Điều này có nghĩa là giao diện người dùng của bạn sẽ vẫn đáp ứng trong khi nhiệm vụ chạy dài đang được thực hiện.

Phương thức `Dowork ()` có hai tham số.Tham số đầu tiên là đối tượng `ProgressChangedEventArgs`.Đối tượng này cung cấp thông tin về tiến trình của nhiệm vụ.Tham số thứ hai là đối tượng `accentEventArgs`.Đối tượng này có thể được sử dụng để hủy bỏ nhiệm vụ.

`` `C#
riêng void Dowork (người gửi đối tượng, ProgressChangedEventArss E)
{
// Thực hiện nhiệm vụ chạy dài ở đây.

// Cập nhật thanh tiến trình.
E.ProTHERPercentage = ProgressBar.value;

// Kiểm tra xem nhiệm vụ đã bị hủy bỏ.
Nếu (e.canceled)
{
// Dừng nhiệm vụ.
công nhân.cancelasync ();
}
}
`` `

Khi nhiệm vụ chạy dài kết thúc, sự kiện `RunworkerCompleted ()` sẽ được nêu ra.Trình xử lý sự kiện này được gọi trên chủ đề chính.Bạn có thể sử dụng trình xử lý sự kiện này để thực hiện bất kỳ nhiệm vụ dọn dẹp hoặc hiển thị kết quả của nhiệm vụ.

`` `C#
Private void worker_runworkerCompleted (người gửi đối tượng, RunworkerCompletedEventArss E)
{
// Nhiệm vụ được hoàn thành.

// Hiển thị kết quả của nhiệm vụ.
nhãn.text = "Nhiệm vụ đã hoàn thành.";
}
`` `

Để biết thêm thông tin về lớp BackidentWorker, vui lòng xem [Tài liệu MSDN] (https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.backgroundworker).

### hashtags

* #C#
* #backgroundworker
* #MultithReading
* #asynchronous
* #Programming
=======================================
#C# #backgroundworker #MultithReading #asynchronous #Programming ### Learn how to use BackgroundWorker in C#

The BackgroundWorker class is a Windows Forms control that allows you to perform long-running tasks in the background while your user interface remains responsive. This can be useful for tasks such as downloading files, uploading files, or processing large amounts of data.

To use the BackgroundWorker class, you first need to create an instance of it in your code. You can do this by calling the `BackgroundWorker()` constructor.

```c#
BackgroundWorker worker = new BackgroundWorker();
```

Once you have created an instance of the BackgroundWorker class, you can start a new task by calling the `RunWorkerAsync()` method. This method takes a delegate as its parameter. The delegate specifies the task that you want to perform in the background.

```c#
worker.RunWorkerAsync(DoWork);
```

The `DoWork()` delegate is a method that you must define. This method is responsible for performing the long-running task. The `DoWork()` method is called on a separate thread from the main thread. This means that your user interface will remain responsive while the long-running task is being performed.

The `DoWork()` method has two parameters. The first parameter is a `ProgressChangedEventArgs` object. This object provides information about the progress of the task. The second parameter is a `CancelEventArgs` object. This object can be used to cancel the task.

```c#
private void DoWork(object sender, ProgressChangedEventArgs e)
{
// Perform the long-running task here.

// Update the progress bar.
e.ProgressPercentage = progressBar.Value;

// Check if the task has been canceled.
if (e.Canceled)
{
// Stop the task.
worker.CancelAsync();
}
}
```

When the long-running task is finished, the `RunWorkerCompleted()` event will be raised. This event handler is called on the main thread. You can use this event handler to perform any cleanup tasks or to display the results of the task.

```c#
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// The task is finished.

// Display the results of the task.
label.Text = "The task is finished.";
}
```

For more information on the BackgroundWorker class, please see the [MSDN documentation](https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.backgroundworker).

### Hashtags

* #C#
* #backgroundworker
* #MultithReading
* #asynchronous
* #Programming
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top