Windows Form Threading Problem in VB.NET
Hi everybody,
I have been developing a VB.NET application using DirectX to display various multimedia within a screen (windows.form). The application displays consecutive screens based on user-defined schedule. In order to fully load the next screen in the background, so that it makes transition between screens going smoothly, I am using Threading technique. However, the screen never appears, in contrast it works fine when I do not use the Threading, but the next screen never loaded properly (some directX components cannot be loaded) and transition does not go smoothly.
Here it is my source code:
Dim ShowFormThread As Thread
Friend WithEvents tm As New System.Windows.Forms.Timer
'frmCanvas is a class inherits from System.Windows.Forms.Form
Friend WithEvents RunForm, OldForm As frmCanvas
Private Sub TickMe()
'timer to trigger this module
tm.Enabled = False
'assign the current/running screen (RunForm) into OldForm
OldForm = RunForm
'call module that assign RunForm into the next screen
'also assign next interval timer
LoopFunc()
'keep the running screen (OldForm) runs at front
If Not OldForm Is Nothing Then
OldForm.BringToFront()
End If
If RunForm Is Nothing Then
tm.Enabled = False
tm.Stop()
Me.Close()
Process.GetCurrentProcess.Kill()
Exit Sub
End If
'this commented part to show the next screen if i do not use Thread method
'''RunForm.WindowState = FormWindowState.Minimized
'''RunForm.Opacity = 1
'''RunForm.Visible = False
'''RunForm.SendToBack()
'''RunForm.Show()
'''RunForm.SendToBack()
'''RunForm.Visible = True
'''RunForm.WindowState = FormWindowState.Maximized
'''RunForm.Location = New System.Drawing.Point(0, 0)
'if start the first screen, i do not use Thread method
If blnStartPlay Then
Call ShowForm()
blnStartPlay = False
Else
'second screen onwards, i use Thread method
If Not ShowFormThread Is Nothing Then
If ShowFormThread.IsAlive Then
ShowFormThread.Abort()
ShowFormThread = Nothing
End If
End If
ShowFormThread = New Thread(AddressOf ShowForm)
ShowFormThread.Start()
'make sure the thread of next screen stop, it means letting the next screen
'fully loaded at background
If ShowFormThread.IsAlive Then
While ShowFormThread.IsAlive
End While
End If
End If
If Not OldForm Is Nothing Then
'make the current/running form (OldForm) moving to right as horizontal transition
Call HorizontalTransition(OldForm.Width)
RunForm.BringToFront()
Dim z As Double
For z = OldForm.Controls.Count - 1 To 0 Step -1
OldForm.Controls(z).Dispose()
Next
OldForm.MainFrm = Nothing
OldForm.Dispose()
OldForm = Nothing
End If
GC.Collect()
If Not Running Then
tm.Stop()
tm.Enabled = False
End If
Thread.Sleep(1000)
tm.Enabled = True
End Sub
Private Sub tm_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tm.Tick
TickMe()
End Sub
Private Sub HorizontalTransition(ByVal intWidth As Integer)
Dim intTransition As Integer
For intTransition = 0 To intWidth Step 100
OldForm.Location = New System.Drawing.Point(intTransition, 0)
Next
Exit Sub
End Sub
Private Sub ShowForm()
RunForm.WindowState = FormWindowState.Minimized
RunForm.Opacity = 1
RunForm.SendToBack()
RunForm.Show()
RunForm.SendToBack()
RunForm.WindowState = FormWindowState.Maximized
RunForm.Location = New System.Drawing.Point(0, 0)
Exit Sub
End Sub
The RunForm at the second screen onwards never appear after I call it using Thread method via method ShowForm.
Please help me on this problem. Thanks a lot in advance.
cheers,
nugie73