newbie thread mishap

  • Thread starter Thread starter e
  • Start date Start date
E

e

Short version:

myThread.Suspend() is suspending my entire application and I don't
understand why.

Long version:

I have a form that I want to pop up frequently throughout my application,
it's essentially a 'please wait' message in a fancy-graphic form. The
program can perform a number of semi-lengthy processes, so I'd like to make
it easy for me to throw up a splash on a whim. It's the only thread I'm
creating in my app.

I thought it would be an interesting learning exercise & experiment to place
this please wait splash screen into a sepereate thread, which I could
suspend & hide / resume & show at will throughout the program. And I
thought the fact that the splash is a little animated might be helped by
running it in a seperate thread. Never the less, I set up a small class
that suspends the thread & hides the form /resumes the thread & shows the
form.

The method that "shows/resumes" is smart enough to know whether or not the
thread needs to be run anew, or simply resumed by checking it's isAlive
status.

When I run the main program eventually the code comes to the first point
where it wants to show a splash screen. It calls the "show/resume" method,
which correctly figures out it needs to run the thread (as opposed to resume
the thread), the form shows up and animates while the main apps processes
work away. The main app finishes what it's doing and calls the
"hide/suspend" method. This appears to suspend both the splash screen and
the main app. I'm just using myThread.Suspend(). Why would that suspend my
entire app?
 
Show some of your code from your class. It sounds like your jsut suspending
the wrong thread.

-CJ
 
Here's the test I'm trying to work with; modMain casts an instance of my
threaded splash class & calls the showSplash() method. It performs a
security check that may take 0.5 to 5.0 seconds or more in the main thread
while the splash runs in the other thread.

------------------

[modMain.vb]

Public frmMain As New clsfrmMain
Public frmSplash As New clsfrmSplash
Public clsThdSplash As New clsThreadedSplash(frmSplash)

Sub Main()

clsThdSplash.showSplash()

If Not theLongSecurityCheck() Then
clsThdSplash.hideSplash()
msgbox("Access denied")
Application.Exit()
Else
clsThdSplash.hideSplash()
End If

Application.Run(frmMain)

End Sub

[End modMain.vb]


[clsThreadedSplash.vb]

Public Class clsThreadedSplash

Private frmThreadsForm As Form
Private ctrlThreadsControl As clsctrlSplashScreen
Private frmForm As Form
Private thdThread As New Threading.Thread(AddressOf
threadedSplashScreen)

Public Sub New(ByRef SplashForm As Form)

frmForm = SplashForm
thdThread.IsBackground = True
thdThread.Name = "splashScreenThread"
thdThread.Priority = Threading.ThreadPriority.Normal

End Sub

Public Sub hideSplash()

frmForm.Hide()
thdThread.Suspend()

End Sub

Public Sub showSplash()

If thdThread.IsAlive Then
Select Case thdThread.ThreadState - 4
Case Is = Threading.ThreadState.Suspended
thdThread.Resume()
End Select
frmForm.Show()
Else
thdThread.Start()
End If

End Sub

Public Sub threadedSplashScreen()

Application.Run(frmForm)

End Sub

End Class

[End clsThreadedSplash.vb]

------------------

Note this all occurs prior to the "main" Application.Run() call in Sub Main,
is that part of my problem? If this code is run, I get the splash... it
animates, it becomes hidden and next I see frmMain; but it's suspended, I
can't work with it, mouse is just an hourglass & no controls appear on it.
Also, if I remove the frmForm.Hide() line from the hideSplash() method, same
results except it ends with both forms on the screen, unresponsive as
described previously. It's almost as if the Application.Run(frmForm) call
in the threadedSplashScreen() member is assuming the role of the main
application thread...? Dunno. If I remove the suspend call all together,
both forms run happily on the screen. So I *could* cheat, and just hide the
splash form and do away with suspending the thread all together, but that
would certainly defeat my intentions of learning about what's going on here.
 
Back
Top