Got two questions.
1. How can I prevent the user from starting two instances of the same program?
2. I am executing a SHELL command from within my program. Is there a way to have my program wait until the SHELL command has completed?
Printable View
Got two questions.
1. How can I prevent the user from starting two instances of the same program?
2. I am executing a SHELL command from within my program. Is there a way to have my program wait until the SHELL command has completed?
1. In Form_Load do
VB Code:
If App.PrevInst Then Unload Me
Added
VB Code:
If App.PrevInstance then Unload me End End If
I get an error when I try and execute a second copy.
RunTime Error 91
Object Variable or with Variable not set
Please post the Form_Load sub.
i'd recommend using Sub Main instead:VB Code:
' In a module Private Sub Main() If Not App.PrevInstance Then frmMain.Show End Sub
VB Code:
Private Sub Form_Load If App.PrevInstance then Unload Me End End If
Rest of Form_Load code runs OK. It is only when I add the code at the top of Form_Load that I get the error.
As long as the App.PrevInstance is the first thing in Form_Load the effect is the same.Quote:
Originally Posted by bushmobile
Is this being done in VB6?Quote:
Originally Posted by JohnDonaldson
I know, but controling the manner in which your app loads is easier in Sub Main and you don't have to think about unloading stuff etc.Quote:
Originally Posted by MartinLiss
2. Try this ShellWait function (need one CommandButton):
VB Code:
Option Explicit Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Const INFINITE = &HFFFF Private Const SYNCHRONIZE = &H100000 Private Const WAIT_TIMEOUT = &H102 Public Sub ShellWait(PathName, Optional WindowStyle As VbAppWinStyle = vbMinimizedFocus, Optional bDoEvents As Boolean = False) Dim dwProcessID As Long Dim hProcess As Long dwProcessID = Shell(PathName, WindowStyle) If dwProcessID = 0 Then Exit Sub End If hProcess = OpenProcess(SYNCHRONIZE, False, dwProcessID) If hProcess = 0 Then Exit Sub End If If bDoEvents Then Do While WaitForSingleObject(hProcess, 100) = WAIT_TIMEOUT DoEvents Loop Else WaitForSingleObject hProcess, INFINITE End If CloseHandle hProcess End Sub Private Sub Command1_Click() ShellWait "notepad.exe", vbNormalFocus MsgBox "Finished !" 'continue code... End Sub
Once again I ask please post your complete code for the Sub. I'm also curious as to why "then" isn't capitalized.
But you don't the other way either.Quote:
Originally Posted by bushmobile
you need to set the start up object to Sub Main.
Project Menu | Project Properties
change the Startup Object combobox to Sub Main
Now I'm confused. The code I posted should be the first line in the Form_Load Sub of your main (first) form. Is it?
I already have a module1.bas , so I added the code to it. Now I get multi-instances.
The code I posted should be the first line in the Form_Load Sub of your main FORM.
I first added the code to the top of my Main form Form_Load. it generates a RunTime Error 91. The first instance runs OK, it is only when I try and execute the program the second time that it pops the RunTime Error 91.
If there is a way to LOCK my program on the screen, so that the user can not move it, the problem becomes mute.
Sorry, but that's not possible. Make an exe from the attached form and you'll only be able to get one instance.Quote:
Originally Posted by JohnDonaldson
both the methods suggest (Form_Load or Sub Main) will work - we'd need to see code to see why they're not working for you.
To stop the form being moved: You can set Moveable = False at design time. Or check out Karl E. Peterson's FormBdr example to change it a runtime.
I tried adding at the top of Form_Load
VB Code:
Private Sub Form_Load Me.Movable = False
It generates this message error
"Function or interface marked as restricted, or function uses an automation type not supported by Visual Basic."
Attach your form.
....Quote:
Originally Posted by bushmobile
Hack.....Just the Form_Load or the entire Form. The entire Form is too big to post here.
Attach the form. If it's too big then put it in a zip file.Quote:
Originally Posted by JohnDonaldson
I am allowed to post the Form_Load but not the entire Form. IP reasons.
VB Code:
Private Sub Form_Load() Dim conPath As String 'conPath = "Data Source=" & App.Path & "\PatientFiles2.mdb;" 'conPath = "Data Source=" & "D:" & "\PatientFiles2.mdb;" conPath = "Data Source=" & "C:" & "\PatientFiles2.mdb;" Set objAccessConnection = New ADODB.Connection Set rsAccess = New ADODB.Recordset 'open connection to server objAccessConnection.Open "PROVIDER=Microsoft.Jet.OLEDB.3.51;" & _ conPath & "Jet OLEDB:Database Password=" 'check connection status If objAccessConnection.State = adStateOpen Then 'MsgBox "Connection open!", 64, "Connection status." connectionopen = True Else MsgBox "Connection not possible.", 64, "Connection status." connectionopen = False End If txtDate.Text = Date txtTime.Text = Time() 'Connect and setup JoyStick Dim rt As Long Dim JoyTestInfo As JOYINFO Dim JoyTestInfo1 As JOYINFO Dim JoyStickCaps As JOYCAPS Dim JoyStickCaps1 As JOYCAPS 'Get the max and min position on the joystick joyGetDevCaps JOYSTICK1, JoyStickCaps, Len(JoyStickCaps) With JoyStickCaps MaxX = .wXmax MinX = .wXmin MaxY = .wYmax MinY = .wYmin End With joyGetDevCaps JOYSTICK2, JoyStickCaps1, Len(JoyStickCaps1) With JoyStickCaps1 MaxX = .wXmax MinX = .wXmin MaxY = .wYmax MinY = .wYmin End With StopButton1 = True 'RootPathName = "D:\" RootPathName = "C:\" 'get the drive's disk parameters Call GetDiskFreeSpaceEx(RootPathName, _ BytesFreeToCaller, _ TotalBytes, _ TotalFreeBytes) 'Read INI file sIniFile = App.Path & "\perkins.ini" sSection = "PlaySnd" Call LoadIniFile sSection = "FootSwitch" Call LoadIniFile frmIntializing.lblNew.Caption = " " ShowPatients VideoCapture = False 'Blank out Icons if no Patients in Database If rsAccess.BOF Or rsAccess.EOF Then 'No more records Image_Delete.Visible = False Image_Update.Visible = False Image_Capture.Visible = False Image_Play.Visible = False Image_DVD.Visible = False End If Disk80P = -1 Disk85P = -1 Me.FrameMenu.BackColor = &HFFECCC Me.lblDate.BackColor = &HFFECCC Me.lblTime.BackColor = &HFFECCC Me.lblDiskSpace.BackColor = &HFFECCC Me.lblPercent.BackColor = &HFFECCC Me.Timer2.Enabled = False 'set Datagrid to highligt row and disable row moving With DataGrid1 .MarqueeStyle = dbgHighlightRow .AllowRowSizing = False End With End Sub
John if you put the PrevInstance code at the top of that and you get an error then something very strange is going on. Did you try the form I attached? If so, what happened when you did?
MartinLess........Your form run OK.
Then if you did the same thing in your project and it's still a problem I'm sorry but there is nothing else I can do for you without your project. If you can get permission you could PM me and I'll give you my email address so you could send it to me.
Thanks anyway.
me.MOVEABLE...notice the E there after the V and before the A? Anyway, set it at design time rather than in form_load...look up "moveable" and set it to false on the form...that should work (did for me)Quote:
Originally Posted by JohnDonaldson
Found a lot easier method. I just removed the title bar by setting "BorderSytle = 0". Then adding to Form_Load
VB Code:
With Me .Top = 0 .Left = 0 End With
Now the user can not move the window and it is locked to 0,0.
Now to disable the Windows Key and ALT+Esc so that the user can not get to the desktop and I'll all set.