|
-
May 20th, 2013, 08:57 PM
#1
suggestions on asynch await and tasks
Hi.
My code:
Code:
Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim mydt As DataTable = Await getds("x")
dg1.ItemsSource = mydt.AsDataView
End Sub
Async Function getds(strx As String) As Task(Of DataTable)
Dim d1 As DataTable = Await GetDsAsync()
Return d1
End Function
Private Shared Function GetDs() As DataTable
Dim strConnString As String = "Data Source=MYPC;Initial Catalog=tests;Integrated Security=True"
Dim con As New SqlClient.SqlConnection(strConnString)
Dim sc As New SqlCommand()
sc.Connection = con
Dim da As New SqlDataAdapter(sc)
da.SelectCommand.Connection = con
da.SelectCommand.CommandText = "select * from doctors"
da.SelectCommand.CommandType = CommandType.Text
Dim dt As DataTable = New DataTable
Try
con.Open()
da.Fill(dt)
Return (dt)
Catch ex As Exception
dt.Clear()
Return dt
Finally
con.Close()
End Try
End Function
Private Shared Function GetDsAsync() As Task(Of DataTable)
Return Task(Of DataTable).Factory.StartNew(Function() getds())
End Function
I am totally new at this so i am not sure that the above is the correct thing to do for an asynchronous method.Had a very hard time on writing the above code because most of what i've read show a method.someasync so it just passes that to await, but i wanted to pass 'whatever' to await and not an already made async method.
I would like to know if there is a better way and if i am doomed to use asynch await in order to have the UI available(i'm assuming that the UI is available this way. If not, let me know) or i should be better of with using tasks and run them asynchronous so i can have better control.If so, is the somehting like 'invoke' required to have the UI available?
I would also like to know how would this be modified if i wanted to block parallel execution,on, let's say a listof GetDsAsync, or i can't do that with await(?)
Thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
May 21st, 2013, 12:32 PM
#2
Addicted Member
Re: suggestions on asynch await and tasks
 Originally Posted by sapator
...but i wanted to pass 'whatever' to await and not an already made async method.
I'm not sure if i understand your question but this method: " Async Function getds(strx As String) As Task(Of DataTable)", seems redundant and does the same thing as your last method "GetDsAsync". The last 2 methods in your example seems to be correct (I didn't test it) for offloading a long running task to the thread pool using Await GetDsAsync() and having the option of calling it synchronously using GetDs(). It doesn't get any simpler than that.
-
May 21st, 2013, 07:49 PM
#3
Re: suggestions on asynch await and tasks
Ye sorry about the double code there.I was excused. What i ask is, let's say i have parallel GetdsAsync functions starting together. How will i do stuff like synclock or priority and such? Is there a threadpool like or i have to re write the code in some other way?
Thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
May 22nd, 2013, 11:20 AM
#4
Addicted Member
Re: suggestions on asynch await and tasks
I'm not really sure how to answer that question without an example of how you would make the parallel requests. However, a few things you might google for that might help you find an answer:
Task.WhenAll, Task.WhenAny, Task.WaitAll, Task.WaitAny
TaskFactory.StartNew -- has parameter for a task scheduler
System.Threading.Interlocked class
System.Threading.Monitor class -- Monitor.Wait, Monitor.Pulse
Free ebook:
Patterns for Parallel Programming
-
May 22nd, 2013, 07:41 PM
#5
Re: suggestions on asynch await and tasks
Will have an extensive look when i have time.I was wondering what is the use of async - await when we start tasks anyhow. Is it just for receiving the end tasks?
Thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
May 23rd, 2013, 09:49 AM
#6
Addicted Member
Re: suggestions on asynch await and tasks
The Await keyword pauses the execution of code at that point on the thread it's it was started on. This allows the UI to resume running while it waits for a Task to be returned to the awaiter. Await keyword must be followed by calling a method that returns a Task or Task(of T) or it will do nothing special. Once a Task is returned -- then the execution of code, if any, will continue below where the Await was called. The most obvious uses for async await is to offload long running tasks to another thread by using TaskFactory.StartNew or the shorthand version Task.Run but it doesn't always mean it has to be used to fire off a new thread. Async await can be used on the UI thread. For example, to await a series of keystrokes or mouse gestures which would use Await Task.WhenAll.
Here Watch this short video. It answers your question best.
-
May 23rd, 2013, 09:01 PM
#7
Re: suggestions on asynch await and tasks
Damn C# examples . Will also have a look, thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|