Menu

How to Safely Cancel an Async Operation in C#

Asynchronous programming is essential for building responsive applications, but it comes with challenges - particularly when you need to cancel operations.

Here's how to safely implement cancellation in C#.

Using CancellationTokenSource

The key to proper cancellation is the CancellationTokenSource class. This provides a token that can be passed to async methods and monitored for cancellation requests.

// Create a cancellation source with timeout
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var token = cts.Token;

try
{
    // Pass token to async operations
    await DoLongRunningTaskAsync(token);
}
catch (OperationCanceledException)
{
    // Handle cancellation gracefully
    Console.WriteLine("Operation was canceled");
}
finally
{
    // Always dispose the CancellationTokenSource
    cts.Dispose();
}

Implementing Cancellation in Your Methods

When writing cancellable async methods, check for cancellation at appropriate points:

async Task DoLongRunningTaskAsync(CancellationToken token)
{
    // Check before starting expensive work
    token.ThrowIfCancellationRequested();
    
    for (int i = 0; i < 100; i++)
    {
        // Periodically check during loops
        if (token.IsCancellationRequested)
        {
            // Clean up resources if needed
            CleanupResources();
            
            // Then throw the standard exception
            throw new OperationCanceledException(token);
        }
        
        await Task.Delay(100, token); // Built-in methods accept tokens
    }
}

Best Practices

  1. Always dispose of CancellationTokenSource objects
  2. Use token.ThrowIfCancellationRequested() for cleaner code
  3. Check for cancellation before expensive operations
  4. Pass the token to all nested async calls
  5. Handle OperationCanceledException appropriately in your calling code

By following these patterns, you can ensure your async operations respond promptly to cancellation requests while maintaining clean, resource-efficient code.

0
1

Related

Removing duplicates from a list in C# is a common task, especially when working with large datasets. C# provides multiple ways to achieve this efficiently, leveraging built-in collections and LINQ.

Using HashSet (Fastest for Unique Elements)

A HashSet<T> automatically removes duplicates since it only stores unique values. This is one of the fastest methods:

List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 };
numbers = new HashSet<int>(numbers).ToList();
Console.WriteLine(string.Join(", ", numbers)); // Output: 1, 2, 3, 4, 5

Using LINQ Distinct (Concise and Readable)

LINQ’s Distinct() method provides an elegant way to remove duplicates:

List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 };
numbers = numbers.Distinct().ToList();
Console.WriteLine(string.Join(", ", numbers)); // Output: 1, 2, 3, 4, 5

Removing Duplicates by Custom Property (For Complex Objects)

When working with objects, DistinctBy() from .NET 6+ simplifies duplicate removal based on a property:

using System.Linq;
using System.Collections.Generic;

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

List<Person> people = new List<Person>
{
    new Person { Name = "Alice", Age = 30 },
    new Person { Name = "Bob", Age = 25 },
    new Person { Name = "Alice", Age = 30 }
};

people = people.DistinctBy(p => p.Name).ToList();
Console.WriteLine(string.Join(", ", people.Select(p => p.Name))); // Output: Alice, Bob

For earlier .NET versions, use GroupBy():

people = people.GroupBy(p => p.Name).Select(g => g.First()).ToList();

Performance Considerations

  • HashSet<T> is the fastest but only works for simple types.
  • Distinct() is easy to use but slower than HashSet<T> for large lists.
  • DistinctBy() (or GroupBy()) is useful for complex objects but may have performance trade-offs.

Conclusion

Choosing the best approach depends on the data type and use case. HashSet<T> is ideal for primitive types, Distinct() is simple and readable, and DistinctBy() (or GroupBy()) is effective for objects.

0
61

Storing passwords as plain text is dangerous. Instead, you should hash them using a strong, slow hashing algorithm like BCrypt, which includes built-in salting and resistance to brute-force attacks.

Step 1: Install BCrypt NuGet Package

Before using BCrypt, install the BCrypt.Net-Next package:

dotnet add package BCrypt.Net-Next

or via NuGet Package Manager:

Install-Package BCrypt.Net-Next

Step 2: Hash a Password

Use BCrypt.HashPassword() to securely hash a password before storing it:

using BCrypt.Net;

string password = "mySecurePassword123";
string hashedPassword = BCrypt.HashPassword(password);

Console.WriteLine(hashedPassword); // Output: $2a$12$...

Step 3: Verify a Password

To check a user's login attempt, use BCrypt.Verify():

bool isMatch = BCrypt.Verify("mySecurePassword123", hashedPassword);
Console.WriteLine(isMatch); // Output: True

Ensuring proper hashing should be at the top of your list when it comes to building authentication systems.

0
174

When working with SQL Server, you may often need to count the number of unique values in a specific column. This is useful for analyzing data, detecting duplicates, and understanding dataset distributions.

Using COUNT(DISTINCT column_name)

To count the number of unique values in a column, SQL Server provides the COUNT(DISTINCT column_name) function. Here’s a simple example:

SELECT COUNT(DISTINCT column_name) AS distinct_count
FROM table_name;

This query will return the number of unique values in column_name.

Counting Distinct Values Across Multiple Columns

If you need to count distinct combinations of multiple columns, you can use a subquery:

SELECT COUNT(*) AS distinct_count
FROM (SELECT DISTINCT column1, column2 FROM table_name) AS subquery;

This approach ensures that only unique pairs of column1 and column2 are counted.

Why Use COUNT DISTINCT?

  • Helps in identifying unique entries in a dataset.
  • Useful for reporting and analytics.
  • Efficient way to check for duplicates.

By leveraging COUNT(DISTINCT column_name), you can efficiently analyze your database and extract meaningful insights. Happy querying!

0
94