How to convert a Task to a Task?

Does such a method exist? No. Is there any overhead to using an async method solely for this purpose? Yes. But it’s the easiest solution. Note that a more generic approach is an extension method for Task such as Then. Stephen Toub explored this in a blog post and I’ve recently incorporated it into AsyncEx. … Read more

UWP update UI from Task

You can use as many tasks as you want, the system will automatically perform queuing and execution for you. That’s one of the advantages of using the built in mechanisms To execute code on the UI thread you can use the dispatcher like this: await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { //UI code here });

Start app at a specific time

You can do it with AlarmManager, heres a short example. First you need to set the alarm: AlarmManager am = (AlarmManager) con.getSystemService(Context.ALARM_SERVICE); Date futureDate = new Date(new Date().getTime() + 86400000); futureDate.setHours(8); futureDate.setMinutes(0); futureDate.setSeconds(0); Intent intent = new Intent(con, MyAppReciever.class); PendingIntent sender = PendingIntent.getBroadcast(con, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); am.set(AlarmManager.RTC_WAKEUP, futureDate.getTimeInMillis(), sender); Next, You need to create a … Read more

Thread.Sleep vs Task.Delay?

The documentation on MSDN is disappointing, but decompiling Task.Delay using Reflector gives more information: public static Task Delay(int millisecondsDelay, CancellationToken cancellationToken) { if (millisecondsDelay < -1) { throw new ArgumentOutOfRangeException(“millisecondsDelay”, Environment.GetResourceString(“Task_Delay_InvalidMillisecondsDelay”)); } if (cancellationToken.IsCancellationRequested) { return FromCancellation(cancellationToken); } if (millisecondsDelay == 0) { return CompletedTask; } DelayPromise state = new DelayPromise(cancellationToken); if (cancellationToken.CanBeCanceled) { state.Registration … 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

Should I worry about “This async method lacks ‘await’ operators and will run synchronously” warning

The async keyword is merely an implementation detail of a method; it isn’t part of the method signature. If a particular method implementation or override has nothing to await, then just omit the async keyword and return a completed task using Task.FromResult<TResult>: public Task<string> Foo() // public async Task<string> Foo() { // { Baz(); // … Read more

Why is Task not co-variant?

According to someone who may be in the know… The justification is that the advantage of covariance is outweighed by the disadvantage of clutter (i.e. everyone would have to make a decision about whether to use Task or ITask in every single place in their code). It sounds to me like there is not a … Read more