如何异步刷新UI [英] How to refresh the UI asynchronously
本文介绍了如何异步刷新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的所有人
下面是一个代码段.
此处的数据获取例程异步运行,但刷新UI(ListView)时则同步运行
谁能帮助我如何异步刷新UI?
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?
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
谢谢
问候
Goutan
Thanks
Regards
Goutan
推荐答案
您只能在UI线程上使用UI控件,所以不能:您不能异步地"执行此操作.
我想您的问题是填充ListView
的循环太慢了吗?您可以尝试使用BeginUpdate
和EndUpdate
调用包装循环:
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 theListView
is too slow? You can try wrapping the loop withBeginUpdate
andEndUpdate
calls:
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
这篇关于如何异步刷新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文