יום רביעי 23 מאי 2012 13:39
I am working with .NET 4.0 and I can't use Microsoft Async/Await features. But I want to add this analog in my application for TASK.
Correct me if I am mistaken.
var task = Task.Factory.StartNew<string>(() => GetURLContents(0)) .ContinueWith(t => this.textBox1.Text = t.Result, TaskScheduler.FromCurrentSynchronizationContext()); MessageBox.Show("Finished");
In this code current thread will be executed further. And Message Finished will be raised before task will be finished.
Of course you can say - use ContinueWith. But I want that this code will be executed in the same thread and after operation is finihed. And I don't want current thread code will be executed further.
You can say - Use task.Wait.
var task = Task.Factory.StartNew<string>(() => GetURLContents(0)) .ContinueWith(t => this.textBox1.Text = t.Result, TaskScheduler.FromCurrentSynchronizationContext()); task.Wait(); MessageBox.Show("Finished");
But task.Wait is blocking current thread. And if I show progress bar while GetURLContents executed UI will be frozen.
But lets look at await
this.textBox1.Text = await GetURLContentsAsync(0) MessageBox.Show("Finished");
As I can understand with Await:
1. Current thread is Not executed further until "Task" is not finished
2. Current thread is Not blocked ( UI is not frozen ) while we waiting result
Q1 :But how can it be ? I read somewhere that it works closely like yeld work and this especial compiler trick. And as I can understand I can't find alternative for it at all.
Q2 :How I can realise such functioanlity in .NET 4.0 ? ( May be I can add some async CTP dll to my project )
Q3: Should I explore ReactiveCommand from Reactive UI and Reactive Extension or with await/async there will be no necessary of them. And await/async will kill them because it will solve the same thing more simplier ?
I create my super player Media Glass
יום רביעי 23 מאי 2012 15:16I find it hard to understand what you're saying. But you can use async-await with .Net 4.0, if you use Visual Studio 11 with the Async Targetting Pack.