tfowler
Regular
I am writing a data acquisition application for our sensor testing and calibration area and have run into a problem. I had the application working great using a single-thread with Do Events in the looping structures, but didn't like the responsiveness of the UI.
So, I did some research and came across the MSDN Magazine Basic Instincts articles on delegates and updating the UI from a secondary thread. It is a great series of articles, which gave me good insight into what I wanted to do. However, implementing it in my application is causing me all kinds of headaches.
I use the following method to update my UI from the data acquisition asynchronous method:
It works great when I update the sbStatus text using the command
. I can update the status text repeatedly with no problems. The problem occurs when the asynchronous method finishes and the UpdateUI method is called from the following callback method:
The application hangs at the line
in the UpdateUI method. I have read everything I can find by Googling and cannot figure out what I am doing wrong.
Thanks for any insight you can provide,
Todd
Edit :
Seems to be rather random.
So, I did some research and came across the MSDN Magazine Basic Instincts articles on delegates and updating the UI from a secondary thread. It is a great series of articles, which gave me good insight into what I wanted to do. However, implementing it in my application is causing me all kinds of headaches.
I use the following method to update my UI from the data acquisition asynchronous method:
Visual Basic:
'---------------------------------------------------------------------
'update the user interface with the status
'
'status - the status text to display in the StatusBar
'state - the state of the data acquisition when this call was made (e.g.
' "Running", "Completed", or "Cancelled")
'---------------------------------------------------------------------
Public Sub UpdateUI(ByVal status As String, ByVal state As String)
If Me.InvokeRequired Then
'need to switch to the primary UI thread to perform update
Dim handler As New UpdateUIHandler(AddressOf UpdateUI)
Dim args() As Object = {status, state}
Me.BeginInvoke(handler, args)
Else
'on primary UI thread, go ahead and update
sbStatus.Text = status
If state = "Completed" Then
btnSave.Visible = True
btnRetry.Visible = True
Beep()
btnSave.Focus()
ElseIf state = "Cancelled" Then
Close()
End If
End If
End Sub
It works great when I update the sbStatus text using the command
Visual Basic:
frmDataAcq.UpdateUI("Change in status text", "Running")
Visual Basic:
'------------------------------------------------------------------
'called when data acquisition process being run on secondary thread
'completes
'
'ar - arguments passed from process
'------------------------------------------------------------------
Private Sub AcquisitionCallBack(ByVal ar As IAsyncResult)
Try
Dim returnValue As Boolean
returnValue = AcquisitionHandler.EndInvoke(ar)
If returnValue = True Then
UpdateUI("Finished", "Completed")
Else
UpdateUI("Finished", "Cancelled")
End If
Catch ex As Exception
Dim message As String
message = "Error: " & ex.Message
UpdateUI(ex.Message, "Cancelled")
End Try
End Sub
The application hangs at the line
Visual Basic:
btnSave.Visible = True
Thanks for any insight you can provide,
Todd
Edit :
Seems to be rather random.
Last edited: