Reputation: 43
This is my first post here, so i hope to be very explicit about my problem.
Currently i'm working with a database, and at some point, i'm having some problems with the latency because of the data amont.
What i want is, when i press a button in the MainForm to call separate Task that takes a method that populates a comboBox in another form, and then display that form.
Here is the code :
Private Sub PopulateCmb()
For Each value As Integer In servRefrence.PopulateID_Masini
Form_Insert.ComboBox1.Items.Add(value)
Next
<<This is taking 3-4 seconds>>
End Sub
Private Async Sub BtnInsert_Click(sender As Object, e As EventArgs) Handles BtnInsert.Click
Dim task As Task = New Task(New Action(AddressOf PopulateCmb))
task.Start()
LblInfo.Text = "Please Wait"
Await task
LblInfo.Text = "Idle"
Form_Insert.Show()
End Sub
This method : servRefrence.PopulateID_Masini is returning a list of integers that comes from the webserver...
The problem with this code is that when the task is finished, the combobox within the Form_Insert comes back empty!
What am i missing??...I think is maybe because you cannot make use of a task within the mainForm(for ex.), and try to "modify" some data in a second form...but i'm not sure...
==I am using Visual Studio 2012== Template : Visual Basic
Please help!
Thank You!!!
Upvotes: 2
Views: 5752
Reputation: 4408
Problem is that you are populating the comboBox in non-UI thread. I guess that latency is caused by service call, so you should populate combobox after task await in UI thread.
Dim task = Task.Factory.StartNew(Of List(Of Integer))(
Function() As List(Of Integer)
Return servRefrence.PopulateID_Masini
End Function)
Dim result = Await task
For Each value As Integer In result
Form_Insert.ComboBox1.Items.Add(value)
Next
Upvotes: 2