|
-
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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
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
|