Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fire a function? 1

Status
Not open for further replies.

Bubbler

IS-IT--Management
Dec 14, 2003
135
US
I have a mod that works as a timer so that my app stays running in the processes with the form hiden and not visible in the task bar. If the mouse is idle for the preset amount of time in the mod, the app shows again.

In order to stop users from launching the app twice, it is common to use:
If App.PrevInstance = True Then

My forms actual function that shows the already running app after the preset idle time is achived is:

If ExpiredMinutes >= MINUTES Then
TimeExpired = 0
frmTimer.Hide
frmMain.Show
Unload frmTimer
End If

This function is what is in the form named frmTimer

My question is how can I cause this function to fire so to speak, In other words if the program is hidden and ExpiredMinutes do not yet equal MINUTES and the user tries to open the executable again (attempt to create another instance of the app), how can I make it so that instead of just killing a second instance, to show or "unhide" the first one for use.
 
>>In order to stop users from launching the app twice, it is common to use:
If App.PrevInstance = True Then
<<

If this still works (I seem to recall some problem with this when I tried it a few years ago), then just pop this code in the form_activate or similar, and use


If App.PrevInstance = True Then
end
endif

to kill the new instance, and allow the old one to continue on it's way.
You could have a messagebox to explain that an instance is already running, if you wished, or one saying 'Thanks for starting the program, I'll be sitting in the systtray quietly until I am needed...'



 
You can change Sub Main() To Form_Load if you wish, also if you put the declare's in a form make them private..

[blue]Option Explicit

Public Const [/blue]GW_HWNDPREV = 3

[blue]Public Declare Function [/blue]OpenIcon [blue]Lib [/blue]&quot;user32&quot; ([blue]ByVal [/blue]hwnd [blue]As Long[/blue]) As Long
[blue]Public Declare Function [/blue]FindWindow [blue]Lib [/blue]&quot;user32&quot; [blue]Alias [/blue]&quot;FindWindowA&quot; ([blue]ByVal [/blue]lpCl[blue]as[/blue]sName As [blue]String[/blue], [blue]ByVal [/blue]lpWindowName [blue]As String[/blue]) As [blue]Long
Public Declare Function [/blue]GetWindow [blue]Lib [/blue]&quot;user32&quot; ([blue]ByVal [/blue]hwnd [blue]As Long[/blue], ByVal wCmd [blue]As Long[/blue]) As Long
[blue]Public Declare Function [/blue]SetForegroundWindow [blue]Lib [/blue]&quot;user32&quot; ([blue]ByVal [/blue]hwnd [blue]As Long[/blue]) As Long

[blue]Public Sub [/blue]Main()
[blue]If [/blue]App.PrevInstance [blue]Then
[/blue]ActivatePrevInstance
[blue]Exit Sub
End If
[/blue]frmMain.Show
[blue]End Sub

Sub [/blue]ActivatePrevInstance()
[blue]Dim [/blue]OldTitle [blue]As String
Dim [/blue]PrevHndl [blue]As Long
Dim [/blue]result [blue]As Long

[/blue]OldTitle = App.Title
App.Title = &quot;unwanted instance&quot;
PrevHndl = FindWindow(&quot;ThunderRTMain&quot;, OldTitle)

[blue]If [/blue]PrevHndl = 0 [blue]Then
[/blue]PrevHndl = FindWindow(&quot;ThunderRT5Main&quot;, OldTitle)
[blue]End If

If [/blue]PrevHndl = 0 [blue]Then
[/blue]PrevHndl = FindWindow(&quot;ThunderRT6Main&quot;, OldTitle)
[blue]End If

If [/blue]PrevHndl = 0 [blue]Then
Exit Sub
End If

[/blue]PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)
result = OpenIcon(PrevHndl)
result = SetForegroundWindow(PrevHndl)
[blue]End
End Sub
[/blue]
 
My question was actually two fold, stop the second instance and then fire the code as I mentioned.

The form is hidden remember and is shown again via this code inside frmTimer
If ExpiredMinutes >= MINUTES Then
TimeExpired = 0
frmTimer.Hide
frmMain.Show
Unload frmTimer
End If

What I am trying to figure out is how to fire the condition TimeExpired = 0
I think that is what I need to do.
 
Beat you to the button I think, reading you above post I'd say 'nah' just use the above code!!
 
Thanks for the code LPlates, but it still starts the app a second time, using either Sub Main() or Form_Load

Maybe I am not looking at this right, when the app is running, it is visible in the process list as CCL.exe, the above example would work if I can kill the first process maybe?
However if we are going to kill the first instance, can we not somehow simplify it all by doing something like:
If App.PrevInstance Then
'Kill the first instance
'Continue to load the new instance
End If


Sorry if this is confusing but it is very hard to explain, but I think this post sums it up. Finally :)
 
If your existing instance has a Window, then you could use Windows API call FindWindow to locate the old window and close it.

But does it matter which instance is running?
If the second instance kills itself because the first one is there, you still have an app running.

Is it then enough to make the running instance continue to run as normal, or are you actually asking for the dialog that appears in the timer to appear anyway if a second instance is started?

If this is the case, perhaps a simpler solution would be:
(Pseudocode)

Start App
Check for previous instance.
If previous instance exists, set 'one time' variable to true
Show the dialog (modally?)
If 'one time' variable is true, end this instance

..then the original app will show its dialog when the timer times out, and the user is none the wiser about which instance they saw




 
Try changing the line...

OldTitle = App.Title

to ...

OldTitle = App.Caption

The above code (my example) works if the caption and title are the same
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top