What's wrong with this code? At the specified time it should close a program, then disconnect from the internet. The FindWindow code works great by itself, as does the internet disconnect portion. However, when they are combined together it fails. First I tried keeping the FindWindow code in a seperate Sub and called it from the first, while I didn't recieve any erros it still didn't close the programs. Then I included the FindWindow code in my original Sub, it will disconnect from the internet but not close the program, and I get VB performed an illegal action & it closes down!

I think that my code isn't waiting long enough for the program to close before moving on to the next section(disconencting from the internet) Any ideas??

Public Sub HangUp(HangUpTime As Date)

On Error GoTo err_main

Dim i As Long
Dim lpRasConn(255) As RasConn
Dim lpcb As Long
Dim lpcConnections As Long
Dim hRasConn As Long
Dim hWindow As Long
Dim lngResult As Long
Dim lngReturnValue As Long

Do
If Now() >= HangUpTime Then

hWindow = FindWindow(vbNullString, "Audiogalaxy Satellite Beta")
lngReturnValue = PostMessage(hWindow, WM_QUIT, vbNull, vbNull)
lngResult = WaitForSingleObject(hWindow, INFINITE)

Call ActiveConnection
If ActiveConnection = True Then
lpRasConn(0).dwSize = RAS_RASCONNSIZE
lpcb = RAS_MAXENTRYNAME * lpRasConn(0).dwSize
lpcConnections = 0
ReturnCode = RasEnumConnections(lpRasConn(0), lpcb, _
lpcConnections)

If ReturnCode = ERROR_SUCCESS Then
For i = 0 To lpcConnections - 1
If Trim(ByteToString(lpRasConn(i).szEntryName)) _
= Trim(gstrISPName) Then
hRasConn = lpRasConn(i).hRasConn
ReturnCode = RasHangUp(ByVal hRasConn)
Unload Me
Exit Sub
End If
Next i
End If
Else
Unload Me
Exit Sub
End If

Else

For i = 1 To 60
Sleep (1000)
DoEvents
Next i

End If

Loop

exit_main:
Exit Sub

err_main:
MsgBox Err.Description, vbOKOnly + vbCritical, "Error #" & Err.Number
Resume exit_main

End Sub


TIA!!!!!!!!!