avoid multiple instance of the same app

Hi,
i've found that wxSingleInstanceChecker is good for me to avoid multiple
instance of the same application to be opened when i double click on the
associates files (associate to my wxpython editor).
But the problem is how to tell to the already running application to open
the new file? how to call a method of the previous application?

thanks

i've found that wxSingleInstanceChecker is good for me to avoid multiple
instance of the same application to be opened when i double click on the
associates files (associate to my wxpython editor).
But the problem is how to tell to the already running application to open
the new file? how to call a method of the previous application?

You could try FindWindowByName (or Id) with a NULL parent, which should
start with all top level windows, then post an event to that window. You'd
have to create your main frame with a unique name that could be found. You'd
also have to create a new event class and create a handler for it.

I haven't tried any of this with wxWindows.
-Rick King

Rick King wrote:

i've found that wxSingleInstanceChecker is good for me to avoid multiple
instance of the same application to be opened when i double click on the
associates files (associate to my wxpython editor).
But the problem is how to tell to the already running application to open
the new file? how to call a method of the previous application?

You could try FindWindowByName (or Id) with a NULL parent, which should
start with all top level windows, then post an event to that window. You'd
have to create your main frame with a unique name that could be found. You'd
also have to create a new event class and create a handler for it.

Just to be clear, you'll have to use platform APIs to do that. The wxWindows functions will only search within the same process.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

wishmaster@sephiroth.it wrote:

Hi,
i've found that wxSingleInstanceChecker is good for me to avoid multiple
instance of the same application to be opened when i double click on the
associates files (associate to my wxpython editor).
But the problem is how to tell to the already running application to open
the new file? how to call a method of the previous application?

You'll need to use some form of IPC to send a message to the current app.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Hello Robin Dunn !

Wednesday, November 26, 2003, 7:19:54 PM, you wrote:

Rick King wrote:

i've found that wxSingleInstanceChecker is good for me to avoid multiple
instance of the same application to be opened when i double click on the
associates files (associate to my wxpython editor).
But the problem is how to tell to the already running application to open
the new file? how to call a method of the previous application?

You could try FindWindowByName (or Id) with a NULL parent, which should
start with all top level windows, then post an event to that window. You'd
have to create your main frame with a unique name that could be found. You'd
also have to create a new event class and create a handler for it.

Just to be clear, you'll have to use platform APIs to do that. The
wxWindows functions will only search within the same process.

This is an os/platform dependent thing.
Prev. instance:

1.) In Windows:
Create Mutex with unique name (Win API), and check it.
When it is loaded, then you must send a message to the prev program.
This program is get message, and load the file.

2.) In Linux:
Create a Locked file (PID), and check the lock.
If locked, then same application is runnning.
But I don't know in the linux how to send a signal to a process to
refresh it's view...

Try this: the new application is create the showthisfiles.txt file in
bin directory, and send a signal to the prev app.
The prev app is load this file, and check the files in it.

I hope this help:
KK

Hi,
i've found that wxSingleInstanceChecker is good for me to avoid multiple
instance of the same application to be opened when i double click on the
associates files (associate to my wxpython editor).
But the problem is how to tell to the already running application to open
the new file? how to call a method of the previous application?

thanks

I've seen somewhere in c.l.p. archive or on activestate site piece of code which illustrates
simple solution. I think you'll be able to find it in archive on
python.org site.
And if you find out a way to do this trick,could you post your
code to this list?
I'm also interested.

···

--
Igor.

This is an os/platform dependent thing.
Prev. instance:

1.) In Windows:
Create Mutex with unique name (Win API), and check it.
When it is loaded, then you must send a message to the prev program.
This program is get message, and load the file.

2.) In Linux:
Create a Locked file (PID), and check the lock.
If locked, then same application is runnning.
But I don't know in the linux how to send a signal to a process to
refresh it's view...

Try this: the new application is create the showthisfiles.txt file in
bin directory, and send a signal to the prev app.
The prev app is load this file, and check the files in it.

Thanks, this is the first way i tryed, but i stop when i have to send the
message to the previous application..

this is the code i was using, but i don't know how to send this message
and which parameter to use in the message...

# ------------
# Main loop
# ------------
class App(wxApp):
    def OnInit(self):
        if wxPlatform == '__WXMSW__':
            hName = win32gui.WNDCLASS()
        wxInitAllImageHandlers()
        self.dlg = Preview()
        self.dlg.Show(True)
        self.timer = StartMainAppTimer(self, self.dlg.text, 2500)
        return True

if wxPlatform == '__WXMSW__':
    myMutex = win32event.CreateMutex(None,-1,'sepy_mutex')
    print win32event.WaitForSingleObject(myMutex, 0)
    if win32event.WaitForSingleObject(myMutex, 0) == 0:
        app = App(0)
        app.MainLoop()
    else:
        exeName = win32api.GetModuleFileName(0)
        sepyWindow = win32gui.FindWindow(0,exeName)
        if not(win32gui.IsWindowVisible(sepyWindow)):
            win32gui.PostMessage(sepyWindow, win32con.WM_USER, 0, 0)
            win32gui.SetForegroundWindow(sepyWindow)
        if len(sys.argv) > 1:
            try:
                // here i should send the message to the previous app
            except:
                print "Errore"
                for line in sys.exc_info():
                    print line
        time.sleep(10)
        sys.exit()
else:
    app = App(0)
    app.MainLoop()

Hello wishmaster@sephiroth.it !

Thursday, November 27, 2003, 1:31:22 PM, you wrote:

Thanks, this is the first way i tryed, but i stop when i have to send the
message to the previous application..

I see your code, and I see errors in it.

So: you want to notify the another instance.
But: the another instance is cannot wait for your next app instances.

The Message/Notify handler is need to be build in your application.
The wxApplication is platform indep. so I don't think it is have a
windows message handler to you, to your code.
Because it is platform dependent thing.

You must hack this with my solutions. The prev application handler
must working in background, because you want to use this program.

KK

wxApp::ProcessMessage

bool ProcessMessage(WXMSG *msg)

Windows-only function for processing a message. This function is called from the main message loop, checking for windows that may wish to process it. The function returns true if the message was processed, false otherwise. If you use wxWindows with another class library with its own message loop, you should make sure that this function is called to allow wxWindows to receive messages. For example, to allow co-existence with the Microsoft Foundation Classes, override the PreTranslateMessage function:

// Provide wxWindows message loop compatibility
BOOL CTheApp::PreTranslateMessage(MSG *msg)
{
  if (wxTheApp && wxTheApp->ProcessMessage((WXMSW *)msg))
    return true;
  else
    return CWinApp::PreTranslateMessage(msg);
}

!!!!!!!!