问题
How to rerun (close then run) my program in vb6
回答1:
The best way to do this is probably to create a "watcher" application that you launch in your main app before terminating it.
First, the main app should create a mutex or semaphore upon launch. When you want to do the app restart, have the main app launch the watcher. The watcher application should wait until the main application mutex/semaphore is gone, then re-launch the main application.
I recently released a semaphore class written in VB6 which would take care of some of the hard work: http://www.vbforums.com/showthread.php?t=634635
回答2:
Stop the currently running executable( click the button with a square block image on it on the IDE ) and then shift+F5, to restart the program.
回答3:
Another approach might be to create a batch file that starts the executable. Call this with an asynchronous call.
You will then need a littlembit of logic to check that you don't have two instances running at once. If there., the new one can wait a second or so. If it is still there, a user warning can be given.
回答4:
Try this.
Main Project :
Private Sub Command2_Click()
Call Shell(App.Path & "\Restart.exe", 1)
End Sub
Restart project :
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const WM_CLOSE = &H10
Private Sub Form_Load()
Dim WinWnd As Long
WinWnd = FindWindow(vbNullString, "Form1") 'Replace Form1 with Whatever your Form's Caption is
If WinWnd <> 0 Then
PostMessage WinWnd, WM_CLOSE, 0&, 0&
Else
MsgBox "No window of that name exists."
End If
Call Shell("Main.exe", 1)
End Sub
found here http://www.vbforums.com/showthread.php?600511-RESOLVED-Application-restarting-itself
来源:https://stackoverflow.com/questions/4579348/how-to-rerun-my-program-in-vb6