VB Looping endlessly

Associate
Joined
9 Sep 2008
Posts
40
Location
Moira, East Midlands
Hey guys,

I have a question regarding my code.

I'm using VB 2008 and have created a project that uses the Windows built in 'cleanmgr' to clean out old files, and then another function runs on to search for spyware.
Each process is set into separate forms - when one completes it opens the next form and closes itself - or at least it should!

"desired process is form 1 opens form 2 (cleanmgr), then form 2 runs - and when complete exits and runs form 3, then 4 and so on"

What happens is form 1 runs the cleanmgr for form2 - but form 1 never closes, and form 2 never displays until the cleanmgr is completed which then displays form 3, then form 4, then form 2, then 3 and so!!

Here's my code:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim clnmgr As Process
clnmgr = Process.Start("cleanmgr", "/sagerun:10101")

clnmgr.WaitForExit(240000)
' if the process doesn't complete within
' 4 minutes, kill it
If Not clnmgr.HasExited Then
clnmgr.Kill()
End If

Form3.Show()
Me.Close()

Any ideas most welcome.

TIA
 
Thats your issue i believe, your Form2 will not close until Form3.Show() has finished doing its thing..

That shouldn't be the issue.
.Show() opens a form asynchronously so that the original thread can carry on processing.
If you call .ShowDialog() that would open it as a modal window and cause the app to hang until you close Form3.

To the OP:
The code looks OK from an initial glance, if try replacing the time out with one second, or something small and step through where is it hanging?
 
Thanks for the tips guys.

I sorted it by placing the function code for any subsequent form within a timer event - and set the timer interval to 5 seconds.

Don't know why but it works, the new form will open as soon as the previous form is done with it's functions?!
 
Back
Top Bottom