Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear All
Below there is a code snippet.
Here the data fetch routine is running asynchronously but when the UI (ListView) is refreshing is running synchronously

Can anyone help me how to refresh the UI asynchronously?

VB
Public Class Form1
    Dim WithEvents oMaster As New Master
    Delegate Sub DataFetchedCallBack(ByVal Status As String)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        oMaster.URL = "http://localhost/JISWeb/MiscData.Asmx"
    End Sub

    Private Sub oMaster_DataFetched(ByVal Status As String) Handles oMaster.DataFetched
        If Me.InvokeRequired Then
            Dim dCallback As New DataFetchedCallBack(AddressOf oMaster_DataFetched)
            Me.Invoke(dCallback, New Object() {Status})
        Else
            Try
                If Status = "OK" Then
                    Label1.Text = "Refreshing"
                    ''''''''''''''''''''''''''''''  <big>This part is running synchronously</big>
                    ListView1.Items.Clear()
                    For i As Integer = 0 To oMaster.Data.Tables(0).Rows.Count - 1
                        'Fill the ListView here
                        Dim Item As New ListViewItem
                        Item.Text = oMaster.Data.Tables(0).Rows(i)("CODE")
                        Item.SubItems.Add(oMaster.Data.Tables(0).Rows(i)("GIVENNAME"))
                        Item.SubItems.Add(oMaster.Data.Tables(0).Rows(i)("SURNAME"))
                        ListView1.Items.Add(Item)

                    Next
                Else
                    MsgBox(Status, MsgBoxStyle.Information)
                End If
                Button1.Enabled = True
            Catch ex As Exception
            End Try
            Label1.Text = "Done"
        End If

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Fetching"
        Button1.Enabled = False
        Dim thr As New System.Threading.Thread(AddressOf RefreshData)
        thr.Start()
    End Sub
    Private Sub RefreshData()
        oMaster.FetchData()
    End Sub
End Class

Public Class Master
    Dim oData As New JISWeb.MiscData
    Public WriteOnly Property URL() As String
        Set(ByVal value As String)
            oData.Url = value
        End Set
    End Property
    Dim dsData As DataSet
    Public Property Data() As DataSet
        Get
            Data = dsData
        End Get
        Set(ByVal value As DataSet)
            dsData = value
        End Set
    End Property
    Public Event DataFetched(ByVal Status As String)
    Public Sub FetchData()
        Try
            dsData = Nothing
            dsData = New DataSet
            dsData = oData.FetchData("SELECT * FROM MEMBERS WHERE CODE < '000000000000310'")
            RaiseEvent DataFetched("OK")
        Catch ex As Exception
            RaiseEvent DataFetched("ERROR " & ex.Message)
        End Try
    End Sub
End Class

Thanks
Regards
Goutan
Posted
Updated 30-Mar-10 22:39pm
v2

1 solution

You can only use UI controls on the UI thread, so no: you can't do that "asynchronously".

I presume your problem is that the loop that fills the ListView is too slow? You can try wrapping the loop with BeginUpdate and EndUpdate calls:

VB
ListView1.BeginUpdate()
ListView1.Items.Clear()
For i As Integer = 0 To oMaster.Data.Tables(0).Rows.Count - 1
  ...
  ListView1.Items.Add( Item )
Next
ListView1.EndUpdate()


Nick
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900