I checked that both....Handle() as well as GetApplication() too. My problem is how do i know one instance of my application is running or not ? e.g if myapp.exe is running which i can see in taskmanager but it is not appearing in windows screen. so when i start is again it should let me know that already one instance is running .
thanks in advance.
You can use the Handle() function with its second argument set to TRUE as under:
IF Handle( This, TRUE ) > 0 THEN
//
MessageBox("Application Already Running", &
This.AppName + " is already running." &
+ " You cannot start it again."
HALT CLOSE
//
ELSE
Open( w_frame )
END IF
However, the above code is for Windows 3.1. If your operating system is later, try the API:
Declare FindWindowA as a global external function:
FUNCTION uint FindWindowA( long classname, string windowname ) LIBRARY "user32.dll"
Then add code like the following to your application's open event:
uint val
//
val = FindWindowA( 0, "MyApp Main Window" )
//
IF val > 0 THEN
MessageBox("Application already running", &
"MyApp is already running. You cannot start it again"
HALT CLOSE
ELSE
open( w_frame)
END IF
my dear friend , I have already tried those PB examples and still I can run multiple instances at a time in one PC.
the fact is that the handle returns 0 values in those cases.
by the way will you please tell me how u defined this function ... please send me the script .
FUNCTION uint FindWindowA( long classname, string windowname ) LIBRARY "user32.dll"
You must be aware that FindWindowA is a Windows' API function. You need to declare it in your app object as a Local External Function and call it in your script just like a PowerBuilder function.
There is no script for this function as the script for this function is provided by Microsoft in the Windows API - User32.dll that every machine has in its Windows directory. All you need to do to call this API function is to declare it as an external function in your app object from the menu:
You will see the Declare menu from the script painter. If you want to declare it as a Global External Function, you can declare it from any object from the Declare menu and it is available and visible through out the application. The code to place in the Declare dialog:
FUNCTION uint FindWindowA( long classname, string windowname ) LIBRARY "user32.dll"
Once you save this declaration in the dialog, you can use this function just like any other PowerBuilder function. At runtime, PowerBuilder resolves this function from the User32.dll of the Windows.
As a matter of fact, functions such as GetFileOpenName(), GetFileSaveName()... are all mapped to the standard Windows' API functions by PowerBuilder for us so we don't have to declare them. However, rare functions such as the FindWindowA have to be declared by us.
Hey if you are not particular about using Handle, Getapplication, api calls etc... i have a very simple solution for you, ofcourse i may be wrong too in giving u this solution as i dont know what ur expectations are anyway here is the solution.
In the application open event open a text file exclusively and if the open of the text file is success then only proceed otherwise give a message saying that already an instance of the application is running. The code would look something as below:
// If some other user is accessing the application, a
//message box is displayed and the
// application will not open, else opens the application.
IF li_filenum=-1 THEN
MessageBox("Hold On", "Application is executing in some other machine ... Please logoff that session and try again"
HALT CLOSE
ELSE
OPEN(w_login)
END IF
My expectaion is to know whether myapplication.exe is running or not ? if it is running then not to allow to open another one. sometimes let's say ...myapplication.exe is running , u can see from TaskMgr but the application
did not appear due to some reason in windows os.
I tried all the way both API , Handle etc . But still I can open another one . All of them gives me return values 0. Anyway I really appreciate for you efforts .
Simple solution... the use of FindWindowA techniques will work only when the Title of the window is same as the one you will be using for this function..
same above ex.
val = FindWindowA( 0, "MyApp Main Window" )
Your window title (through properties you can set that) should be "MyApp Main Window"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.