Share Học cách sử dụng Async và Await trong lập trình C#

catlinhchainsaw

New member
#C ##Async #Await #asynchronous #Concurrency ## Tìm hiểu cách sử dụng async và chờ đợi trong lập trình C #

Async và Await là các từ khóa trong ngôn ngữ lập trình C# cho phép bạn viết mã không đồng bộ.Mã không đồng bộ là mã không chặn luồng gọi trong khi nó đang chờ hoàn thành một tác vụ.Điều này có thể hữu ích để cải thiện hiệu suất của các chương trình của bạn, vì nó cho phép các nhiệm vụ khác chạy trong khi nhiệm vụ không đồng bộ đang chờ xử lý.

Để sử dụng Async và chờ đợi, trước tiên bạn cần phải khai báo một phương thức là không đồng bộ bằng cách sử dụng từ khóa `async`.Sau đó, bạn có thể sử dụng từ khóa `đang chờ đợi để chờ hoàn thành tác vụ không đồng bộ.Ví dụ: mã sau không đồng bộ đọc một tệp từ đĩa:

`` `C#
async tác vụ readfileasync (tên tệp chuỗi)
{
// Tạo một tác vụ để đọc tệp từ đĩa.
TASK

// Đợi nhiệm vụ hoàn thành.
Chuỗi nội dung = đang chờ Readtask;

// Làm điều gì đó với nội dung của tệp.
Console.WriteLine (Nội dung);
}
`` `

Khi bạn gọi phương thức `readFileAnync`, từ khóa 'đang chờ' sẽ đình chỉ việc thực hiện phương thức hiện tại và chờ hoàn thành` readtask`.Khi `readtask` đã hoàn thành, từ khóa` Await` sẽ tiếp tục thực hiện phương thức hiện tại và biến `Nội dung` sẽ được điền với nội dung của tệp.

ASYNC và AIDIT có thể được sử dụng để cải thiện hiệu suất của các chương trình của bạn bằng cách cho phép các tác vụ khác chạy trong khi một nhiệm vụ không đồng bộ đang chờ xử lý.Chúng cũng có thể được sử dụng để làm cho mã của bạn dễ đọc và có thể duy trì hơn.

## Tài nguyên bổ sung

* [Async và chờ đợi trong tài liệu C#] (Asynchronous programming in C# - C#)
* [Async và chờ đợi trong C# Hướng dẫn] (https://www.tutorialspoint.com/csharp/csharp_async_await.htm)
* [Async và chờ đợi trong bài viết trên blog C#] (https://devblogs.microsoft.com/dotnet/async-await-in-cy-sharp/)
=======================================
#C# #Async #Await #asynchronous #Concurrency ## Learn how to use Async and Await in C# Programming

Async and Await are keywords in the C# programming language that allow you to write asynchronous code. Asynchronous code is code that does not block the calling thread while it is waiting for a task to complete. This can be useful for improving the performance of your programs, as it allows other tasks to run while the asynchronous task is pending.

To use Async and Await, you first need to declare a method as asynchronous using the `async` keyword. You can then use the `await` keyword to wait for the asynchronous task to complete. For example, the following code asynchronously reads a file from disk:

```c#
async Task ReadFileAsync(string filename)
{
// Create a Task to read the file from disk.
Task<string> readTask = Task.Run(() => File.ReadAllText(filename));

// Wait for the Task to complete.
string contents = await readTask;

// Do something with the contents of the file.
Console.WriteLine(contents);
}
```

When you call the `ReadFileAsync` method, the `await` keyword will suspend the execution of the current method and wait for the `readTask` to complete. Once the `readTask` has completed, the `await` keyword will resume the execution of the current method and the `contents` variable will be populated with the contents of the file.

Async and Await can be used to improve the performance of your programs by allowing other tasks to run while an asynchronous task is pending. They can also be used to make your code more readable and maintainable.

## Additional resources

* [Async and Await in C# documentation](https://docs.microsoft.com/en-us/dotnet/csharp/async/)
* [Async and Await in C# tutorial](https://www.tutorialspoint.com/csharp/csharp_async_await.htm)
* [Async and Await in C# blog post](https://devblogs.microsoft.com/dotnet/async-await-in-c-sharp/)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top