What is the equivalent of FromCanceled(CancellationToken) in .NET 4.5?

Advertisements I want to achieve something similar to this in .NET 4.5 public virtual Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { if (!cancellationToken.IsCancellationRequested) { return BeginEndWriteAsync(buffer, offset, count); } return Task.FromCancellation(cancellationToken); } Task.FromCanceled(CancellationToken) method is available in .NET 8. I would like to know its equivalent in .NET 4.5. >Solution : In… Read More What is the equivalent of FromCanceled(CancellationToken) in .NET 4.5?

What's the difference between Task.Run and async Task in this context?

Advertisements Could someone please explain to me what is the difference in the behaviour of the two code samples below? Which one is better/safer? This is a snippet from a Repository implementation. Task.Run public Task<TDocument> FindByIdAsync(string id) { return Task.Run(() => { var objectId = new ObjectId(id); var filter = Builders<TDocument>.Filter.Eq(doc => doc.Id, objectId); return… Read More What's the difference between Task.Run and async Task in this context?

Why my Task.WhenAll is not waiting until completion?

Advertisements I’m running into an issue here and you might know the answer: I just couldn’t figure it out still. I’m creating a seed for a simple scenario here: adding likes to a given post. The method signature is: async Task GeneratePostLike(string postId, string userId, CancellationToken cancellationToken = default) { return await postLikeService.AddUserLikeToPost( new PostLikeInput… Read More Why my Task.WhenAll is not waiting until completion?

Some functions inside `Task` closure becomes an async function in Swift

Advertisements I’m not sure how is this being triggered, but sometimes when I call a function inside Task { } XCode gives an error that instructs me to add await in the called function. Here’s a simplified instance I experienced: public class Loader: UIView { public class func show(animated: Bool = true) { // showing… Read More Some functions inside `Task` closure becomes an async function in Swift

How to build relevant auto generating tags recommendation model in python

Advertisements How to Build a Relevant Auto Generating Tags Recommendation Model in Python One of the most important features of any blog or website is its ability to recommend relevant tags to users. This not only helps users find related content easily, but it also improves the overall user experience. In this blog post, we’ll… Read More How to build relevant auto generating tags recommendation model in python

Cannot override a method that returns a Task<T?>

Advertisements Consider the following code: public class A { public virtual Task<T?> M<T>() { throw new NotImplementedException(); } } public class B : A { public override Task<T?> M<T>() { throw new NotImplementedException(); } } This will not compile, with the following error: CS0508: ‘B.M<T>()’: return type must be ‘Task<T?>’ to match overridden member ‘A.M<T>()’… Read More Cannot override a method that returns a Task<T?>

Do async calls within a function marked with @MainActor also run on the main thread?

Advertisements I have a function marked with @MainActor that makes an async call, then updates a @Published var that will subsequently update the UI. Is the async call blocking on the main thread, or is the updating of the published var the only thing that will be done on the main thread? @Published var users:… Read More Do async calls within a function marked with @MainActor also run on the main thread?

Write a program that takes three numbers as input and outputs the maximum of those numbers (C#)

Advertisements struggling with this task. What’s wrong with my code? Thank you in advance Console.Clear(); int number1 = int.Parse(Console.ReadLine()); int number2 = int.Parse(Console.ReadLine()); int number3 = int.Parse(Console.ReadLine()); max = 0; if (number1 > number2) { max = number1; } if (number1 < number2) { max = number2; } if (max > number3) { Console.WriteLine(max); }… Read More Write a program that takes three numbers as input and outputs the maximum of those numbers (C#)