J
Jeremy Holt
Hi,
In a windows.forms application I would BeginInvoke a delegate on the UI
thread to collect data from a database. When the call returns to the
AsyncCallback, if the Control.InvokeRequired = True, I would then have the
Control.BeginInvoke(New AsyncCallback(AddressOf GetDataCallback), New
Object() {ar}).
How would one achieve the same thing on an asp.net page (without using a
webseervice)? In the code below, because the GetDataCallBack returns on a
thread different to the one originally invoked, the DataGrid will not
update.
Private da As New clsYields
Private Delegate Function GetDataDelegate(ByVal Crop As Integer) As dsYields
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Debug.WriteLine("Page load " &
Threading.Thread.CurrentThread.GetHashCode)
End Sub
Private Function GetData(ByVal Crop As Integer) As dsYields
Return Me.da.GetData(Crop)
End Function
Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
Dim deleg As GetDataDelegate = CType(ar.AsyncState,
GetDataDelegate)
Dim ds As dsYields = deleg.EndInvoke(ar)
Me.DataGrid1.DataSource = ds.Production.DefaultView
Me.DataBind()
Debug.WriteLine("Data loaded " &
Threading.Thread.CurrentThread.GetHashCode)
End If
Catch ex As Exception
Tools.WriteException(ex)
End Try
End Sub
' Windows.Forms application
Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
If Me.Form1.InvokeRequired Then
Me.Form1.BeginInvoke(New AsyncCallback(AddressOf
GetDataCallback), New Object() {ar})
Else
Dim deleg As GetDataDelegate = CType(ar.AsyncState,
GetDataDelegate)
Dim ds As dsYields = deleg.EndInvoke(ar)
Me.DataGrid1.DataSource = ds.Production.DefaultView
Debug.WriteLine("Data loaded " &
Threading.Thread.CurrentThread.GetHashCode)
End If
End If
Catch ex As Exception
Tools.WriteException(ex)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim deleg As New GetDataDelegate(AddressOf GetData)
deleg.BeginInvoke(2005, New AsyncCallback(AddressOf GetDataCallBack),
deleg)
End Sub
Many thanks in advance
Jeremy
In a windows.forms application I would BeginInvoke a delegate on the UI
thread to collect data from a database. When the call returns to the
AsyncCallback, if the Control.InvokeRequired = True, I would then have the
Control.BeginInvoke(New AsyncCallback(AddressOf GetDataCallback), New
Object() {ar}).
How would one achieve the same thing on an asp.net page (without using a
webseervice)? In the code below, because the GetDataCallBack returns on a
thread different to the one originally invoked, the DataGrid will not
update.
Private da As New clsYields
Private Delegate Function GetDataDelegate(ByVal Crop As Integer) As dsYields
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Debug.WriteLine("Page load " &
Threading.Thread.CurrentThread.GetHashCode)
End Sub
Private Function GetData(ByVal Crop As Integer) As dsYields
Return Me.da.GetData(Crop)
End Function
Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
Dim deleg As GetDataDelegate = CType(ar.AsyncState,
GetDataDelegate)
Dim ds As dsYields = deleg.EndInvoke(ar)
Me.DataGrid1.DataSource = ds.Production.DefaultView
Me.DataBind()
Debug.WriteLine("Data loaded " &
Threading.Thread.CurrentThread.GetHashCode)
End If
Catch ex As Exception
Tools.WriteException(ex)
End Try
End Sub
' Windows.Forms application
Private Sub GetDataCallBack(ByVal ar As IAsyncResult)
Try
If ar.IsCompleted Then
If Me.Form1.InvokeRequired Then
Me.Form1.BeginInvoke(New AsyncCallback(AddressOf
GetDataCallback), New Object() {ar})
Else
Dim deleg As GetDataDelegate = CType(ar.AsyncState,
GetDataDelegate)
Dim ds As dsYields = deleg.EndInvoke(ar)
Me.DataGrid1.DataSource = ds.Production.DefaultView
Debug.WriteLine("Data loaded " &
Threading.Thread.CurrentThread.GetHashCode)
End If
End If
Catch ex As Exception
Tools.WriteException(ex)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim deleg As New GetDataDelegate(AddressOf GetData)
deleg.BeginInvoke(2005, New AsyncCallback(AddressOf GetDataCallBack),
deleg)
End Sub
Many thanks in advance
Jeremy