System.Threading.Tasks – Limit the number of concurrent Tasks

I know this is almost a year old, but I have found a much easier way to achieve this, so I thought I would share: Dim actionsArray() As Action = new Action(){ New Action(Sub() DoComputation1()), New Action(Sub() DoComputation2()), … New Action(Sub() DoComputation100()) } System.Threading.Tasks.Parallel.Invoke(New Tasks.ParallelOptions() With {.MaxDegreeOfParallelism = 5}, actionsArray) Voila!

IRequestHandler return void

Generally speaking, if a Task based method does not return anything you can return a completed Task public Task Handle(CreatePersonCommand message, CancellationToken cancellationToken) { return Task.CompletedTask; } Now, in MediatR terms a value needs te be returned. In case of no value you can use Unit: public Task<Unit> Handle(CreatePersonCommand message, CancellationToken cancellationToken) { return Task.FromResult(Unit.Value); … Read more

What happens while waiting on a Task’s Result?

In Windows, all I/O is asynchronous. Synchronous APIs are just a convenient abstraction. So, when you use HttpWebRequest.GetResponse, what actually happens is the I/O is started (asynchronously), and the calling thread (synchronously) blocks, waiting for it to complete. Similarly, when you use HttpClient.PostAsync(..).Result, the I/O is started (asynchronously), and the calling thread (synchronously) blocks, waiting … Read more

App always starts fresh from root activity instead of resuming background state (Known Bug) [duplicate]

This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category. Whoever submitted … Read more

Set ApartmentState on a Task

When StartNew fails you just do it yourself: public static Task<T> StartSTATask<T>(Func<T> func) { var tcs = new TaskCompletionSource<T>(); Thread thread = new Thread(() => { try { tcs.SetResult(func()); } catch (Exception e) { tcs.SetException(e); } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); return tcs.Task; } (You can create one for Task that will look almost identical, or add … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)