How to launch an instance of the app already running? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

How to launch an instance of the app already running?

Post by Louigi Verona »

I would like to fire up another instance of the app by, say, pressing a key. Is that possible and if yes - then how? I did search the forum and wiki and documentation. Found wxExecute, but I am not sure this is the right thing.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

Your app, or any app?
"Keyboard not detected. Press F1 to continue"
-- Windows
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

My app - the app that I am currently running. For instance, I am running Notepad and from within Notepad I want to be able to press a key and another Notepad opens.
Romas
I live to help wx-kind
I live to help wx-kind
Posts: 176
Joined: Mon Jun 16, 2008 11:07 am
Location: Kaunas

Post by Romas »

If the working platform is Windows, all you need is to hook keyboard events (you still need a monitoring program to handle that hook [your program can do this too]). But, if you need multiplatform thing I am out of ideas :)
Everything requires a line of code.
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

Yeah, I need multiplatform. Funny if this is not possible - seems like simple functionality.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

if you just want to start your own application from your own code again:

Code: Select all

::wxExecute( wxGetApp().argv[0] );
But you must take care of resources that are used multiple times, like configuration files etc. so that the instances don't conflict with each other.
Use the source, Luke!
Romas
I live to help wx-kind
I live to help wx-kind
Posts: 176
Joined: Mon Jun 16, 2008 11:07 am
Location: Kaunas

Post by Romas »

I've understood, that Louigi Verona wants to start the app from different process, but with particular hot key. Did I understand correctly?
Everything requires a line of code.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

See wiki article about catching key events globally; then just do what doublemax said (even though i'm not sure relying to arg[0] is a good idea)
"Keyboard not detected. Press F1 to continue"
-- Windows
mikeandtherest
Experienced Solver
Experienced Solver
Posts: 50
Joined: Fri Jul 24, 2009 5:27 pm
Location: Cluj-Napoca, Romania

Post by mikeandtherest »

I think Auria is right. As far as I know, argv[0] only stores the name of the executable file. So if the current directory of your application is changed during the execution, then you won't be able to execute your application again because you won't have the correct path. This means: don't rely on the argv[0], put the full path instead.

Cheers,
Mihai Paraschivescu
"The Journey Of A Thousand Miles Begins With A Single Step"
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Post by doublemax »

at least on Windows argv[0] always contains the whole path. If in doubt, there's always wxStandardPaths::GetExecutablePath
Use the source, Luke!
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

Okay, thanks guys, I will try it.

My programm already can catch key events globally, so there shouldn't be much problem!
Louigi Verona
Earned some good credits
Earned some good credits
Posts: 127
Joined: Tue Mar 24, 2009 10:21 am
Contact:

Post by Louigi Verona »

When used the code you wrote, got an error: wxGetApp undeclared.

However, I tried this and it worked:

Code: Select all

::wxExecute(wxTheApp->argv[0]);
thanks for the help, doublemax!
Post Reply