Begin an rant… And more… on VisualStudio 2005.

The first thing I wanted to do was to create a new thread to do some work. So the first thing that I need to do is to create a thread – an empty one. While we are at it we will also create a semaphore to indicate when the thread should be run.

Private thdLiveUpdate As Thread
Private Semaphore as Boolean = False

Then we just need to tell the thread to run out code… creating an instance of the thread if needed.

private sub RunThread()
If thdLiveUpdate Is Nothing Then
thdLiveUpdate = New Thread(AddressOf ThreadLiveUpdate)
End If
Semaphore = True
End Sub

This next bit of code will run a thread called ThreadLiveUpdate whenever Semaphore is true. In this case UpdateStatusBar is also important since we need to update the GUI from the thread.

Private Sub ThreadLiveUpdate()
While True
If Semaphore Then
UpdateStatusBar("Updating Positions")
DoSomethingUseful()
UpdateStatusBar("")
Semaphore = False
End If
System.Threading.Thread.Sleep(1000)
End While
End Sub


We wanted to use Me.StatusBar1.Text = “StatusBar1”, but this would be running in the wrong thread. So we get the update to run in another thread. Do do this we create a ‘Delegate’

Delegate Sub UpdateStatusBarCallback(ByVal [TEXT] As String)

Then we use the InvokeRequired to see if the function is in the correct thread space. Strangely I could not find an InvokeRequired function in the StatusBar that I was using so I used another graphical element.

Private Sub UpdateStatusBar(ByVal [TEXT] As String)
If Me.grdOutlook.InvokeRequired Then ' Not the right onject but it works
Dim d As New UpdateStatusBarCallback(AddressOf UpdateStatusBar)
Me.Invoke(d, New Object() {[TEXT]})
Else
Me.StatusBar1.Text = [TEXT]
End If
End Sub

As I mentioned, I have a rant too… The problem is that I cannot work out how to move items on the TabStrip. It does not behave itself. Notice the place of the menu on the graphic below? I cannot work out how to move it… Can you spell BUG?