Can not restart computer untill VB program is closed

paul viscovich
09-08-2003, 09:28 AM
I have what seems like a simple problem,

When i have a vb6 program running on a workstation or a server nt4.0, 2k or xp I have to close the program manually before the system can be rebooted or shut down.

If I select restartor shutdown I get a simple message along the lines of can't quit, If I close the VB Program then I can restart normally

Suggestions?

Thinker
09-08-2003, 09:33 AM
What code do you have in the Unload or QueryUnload event subs in your
forms?

paul viscovich
09-08-2003, 09:43 AM
What code do you have in the Unload or QueryUnload event subs in your
forms?

Well Actually I had to do a little reading to see what you were asking. I would say I have no code in those event subs?

No really I did not fully understand what you asked.

Thinker
09-08-2003, 09:45 AM
There are only two reasons why I can see that windows would have
trouble stopping your program. One, you set Cancel = True in one of
those event subs. Two, you aren't cleaning up some object and it is
getting stuck in memory. Like a form that gets loaded but never unloaded.

paul viscovich
09-08-2003, 10:25 AM
There are only two reasons why I can see that windows would have
trouble stopping your program. One, you set Cancel = True in one of
those event subs. Two, you aren't cleaning up some object and it is
getting stuck in memory. Like a form that gets loaded but never unloaded.

So I soppose the below code would work to start with. I do not care if I toss anything open files ect. due to the reboot. I could also just close everything up on exit Do you this this is what you are suggesting.

Unload Event Example
This example demonstrates a simple procedure to close a form while prompting the user with various message boxes. In an actual application, you can add calls to general purpose Sub procedures that emulate the processing of the Exit, Save, and Save As commands on the File menu in Visual Basic. To try this example, paste the code into the Declarations section of a form, and then press F5. Once the form is displayed, press ALT+F4 to close the form.

Private Sub Form_Unload (Cancel As Integer)
Dim Msg, Response ' Declare variables.
Msg = "Save Data before closing?"
Response = MsgBox(Msg, vbQuestion + vbYesNoCancel, "Save Dialog")
Select Case Response
Case vbCancel ' Don't allow close.
Cancel = -1
Msg = "Command has been canceled."
Case vbYes
' Enter code to save data here.
Msg = "Data saved."'
Case vbNo
Msg = "Data not saved."
End Select
MsgBox Msg, vbOKOnly, "Confirm" ' Display message.
End Sub

passel
09-08-2003, 10:35 AM
Yes, basically.

Since you don't care to save anything, you just need to clean up so
that your program will exit. Usually the problem, is along these lines.

You have a simple endless loop that is doing "background" processing.
You need to have the loop checking a flag to see when it's time to exit,
so that your program can close normally. Something along these lines.

Dim Time_to_Exit as Boolean

'Your loop
Do until Time_to_Exit
' your code
Loop
'Unload any forms you have active and any other dynamically allocated
'"stuff"
Unload Me

Private Sub Form_Unload (Cancel As Integer)
if Not Time_to_Exit then 'if the loop is still active then
Time_to_Exit = True ' inform the loop to exit
Cancel = 1 ' cancel the unload for now, until after the loop
end if
end sub

Once the loop exits, it will do the unload again, this time Time_to_Exit
will be true, and you should exit normally.

paul viscovich
09-08-2003, 06:03 PM
Ok I am Lost. I soppose I just don't get what I am looking for

Here is the code I have small program that is loaded at login and waits 2 min. before it runs

If i perform a log out shutdown or reboot it hanges the program and presents a message that it can not shut down. It the program is running and I reboot I would like it to just simple end itself and close dowan so that windows may reboot or shut down. No messages just go away.


Dim SourceFile, DestinationFile, a, b, s, Server, ver, Wait1, Wait2, RunYN As String
Dim temp, x, x1 As Integer
' *****************************
' ** (C) Paul Viscovich 2003 **
' *****************************

Private Sub Form_Load()

On Error Resume Next
Open "c:\inv\control.ini" For Input As #1
If Err.Number = 53 Then
Msg = "There was an error attempting to open the Control.ini File!"
MsgBox Msg, , "Inventory Program Error"
Err.Clear ' Clear Err object fields
On Error GoTo 0
End
End If
Line Input #1, ver
Line Input #1, Server
Line Input #1, Wait1
Line Input #1, Wait2
Line Input #1, RunYN
Close #1

Sleep (Wait1 * 1000)

s = "\\" + Server + "\inventory\scripts\control.ini"

Open s For Input As #1
Line Input #1, a
Close #1
If a = "" Then
Msg = "There was an error with the File Server " + Server
MsgBox Msg, , "Inventory Program Error"
End
End If

If Not FileExists("c:\inv\post.exe") Then GoTo skip0
If a = ver Then
x = Shell("c:\inv\post.exe", vbHide)
End
End If

skip0:
s = "\\" + Server + "\inventory\scripts\kill.txt"
On Error Resume Next
Open s For Input As #1
If Err.Number = 53 Then GoTo skip1

Do While Not EOF(1)
Line Input #1, a
DestinationFile = "c:\inv\" + a
Kill DestinationFile
Loop

skip1:
On Error GoTo 0
Close #1

s = "\\" + Server + "\inventory\scripts\copy.txt"
On Error Resume Next
Open s For Input As #1
If Err.Number = 53 Then GoTo Skip2

Do While Not EOF(1)
Line Input #1, a
SourceFile = "\\" + Server + "\inventory\scripts\" + a
DestinationFile = "c:\inv\" + a
FileCopy SourceFile, DestinationFile
Loop

Skip2:
On Error GoTo 0
Close #1

Sleep (Wait2 * 1000)

skip3:
If FileExists("c:\inv\post.exe") = False Then
Msg = "There was an error attempting to open the Post.exe File!"
MsgBox Msg, , "Inventory Program Error"
Err.Clear ' Clear Err object fields
On Error GoTo 0
End
End If

On Error Resume Next
Open "c:\inv\control.ini" For Input As #1
If Err.Number = 53 Then
Msg = "There was an error attempting to open the Control.ini File!"
MsgBox Msg, , "Inventory Program Error"
Err.Clear ' Clear Err object fields
On Error GoTo 0
End
End If
Line Input #1, ver
Line Input #1, Server
Line Input #1, Wait1
Line Input #1, Wait2
Line Input #1, RunYN
Close #1

If RunYN = "NO" Then End
x = Shell("c:\inv\post.exe", vbHide)
End

End Sub

' *****************************
' ** (C) Paul Viscovich 2003 **
' *****************************

Function FileExists(strFile As String) As Integer

Dim lSize As Long
On Error Resume Next
lSize = -1
'Get the length of the file
lSize = FileLen(strFile)
If lSize < 0 Then FileExists = False Else FileExists = True

End Function

passel
09-08-2003, 08:05 PM
I think the problem here is that you are using Sleep. Sleep actual stops
your program until awakened up by the operating system, so you can
receive no events during the time you are asleep. I don't know if that
includes the shutdown, it seems that should overide the sleep, but I don't know.

Perhaps, rather than put your program to sleep, add a timer, and set the
timer interval to the value input. The timer will fire at that time, and
you could then do what you need to do. I haven't looked at the code
in detail, so I don't know if there are any other issues. I'll look at it a
little closer now.

Edit:
Ok, I looked at it a little bit. I think if you just created a "State Machine"
and used a timer, that should work well for you.

So, create a State Variable, move your code out of the Form load and
into a subroutine.

In the Form_Load, set the State Variable to 1, for the first state and
call you subroutine. (Process_Routine for example)

In the subroutine, do a Select Case on your State Variable.

Private Sub Process_Routine

Select Case State_Variable
Case 1
Do your initial stuff
Set your timer interval to wait1*1000 interval
Set your State_Variable to 2
Enable your timer (i.e. Timer1.Enabled = True)

Case 2
Do your case 2 stuff
Set the State_Variable
set the timer
enable the timer

Case 3
' etc....

End Select

'In your timer routine

Sub Timer1_Timer
Timer1.Enabled = False
Process_Routine
end

This way, your program will be awake, but idle, and should shutdown
normally, when it receives the Shutdown message.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum