0

I have a WPF application in which i have to do a long running task (basically reading from network). Just to give you a snapshot I am doing the following thing on button click

 Dim t As Task(Of String) = Task.Factory.StartNew(Of String)(Function()
                                                                        'Thread.sleep is simulating long running task that will make UI unresponsive
                                                                        Thread.Sleep(10000)
                                                                        Return "Hello world from async task"
                                                                    End Function)

        TextBlock1.Text = t.Result

I cannot use Event based async methodology because the reading API actually exist in a dll which i refer in my program, that contains a function Public function ReadFromNetwork() as String. This API is making an async call to network to read a long string and return to UI. So, in short i m doing TextBlock1.Text = ExternalDll.ReadFromNetwork().

but the problem is that even if i use Task asynchrony, the UI is still unresponsive.

Can you please detect if i m missing something in code.

Any help/suggestion will be highly appreciated Thanx in advance

3 Answers 3

8

You're using t.Result on the line after you start the task. That will make the thread block until the task completes - so all the asynchrony is in vain.

You should attach a continuation using Task.ContinueWith, and put the code using the task result into that continuation. That will allow the UI to go back to handling events while the task is executing, and then your continuation will be fired when the task has completed. Pass in TaskScheduler.FromCurrentSynchronizationContext to make sure that the continuation fires on the right thread.

Note that in the next version of VB/C#, all this will be much easier with async methods. If you're able to use the .NET 4.5 release candidate, you should consider trying this right now - it'll make your life much simpler.

Sign up to request clarification or add additional context in comments.

6 Comments

Yes jon, i know... .net 4.5 is bringing async and await keywords to language. but my app is inherently to be used by pre-windows 8 operating systems.
@Uday0119: .NET 4.5 isn't restricted to Windows 8.
Now i am getting an exception as The calling thread cannot access this object because a different thread owns it. on accessing TextBlock from ContinueWith method
oops, my bad, i did not read for SynchContext thing you wrote. Now its running properly.
+1. Definitely worth considering the 4.5 release candidate. Despite the name Microsoft support its use in production environments including Windows 7 and Windows Server 2008
|
2

You are calling t.Result which will force the Task to execute. You should use ContinueWith to get the result asynchronously.

    var action = delegate(Task<string> s)  // to avoid cross thread exception 
        {
            Action ac = delegate() { TextBlock1.Text = s.Result; };
            this.Dispatcher.Invoke(ac);

        };

      t.ContinueWith(action);

excuse me for using c#

Comments

1

I think it should be something like

t.ContinueWith((result) => { TextBlock1.Text = result.Result});

Or did you just loose it during posting? You may have to use the correct Scheduler, other than that this should do the trick

2 Comments

Now i am getting an exception as The calling thread cannot access this object because a different thread owns it. on accessing TextBlock from ContinueWith method
Check the overload Task<TNewResult> ContinueWith<TNewResult>(Func<Task<TResult>, TNewResult> continuationFunction, TaskScheduler scheduler); You can pass the UI scheduler that you can grap using var uithread = TaskScheduler.FromCurrentSynchronizationContext();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.