launch second application with a new thread 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
SalmonsSteve
Knows some wx things
Knows some wx things
Posts: 27
Joined: Sun Jun 24, 2012 6:00 pm

launch second application with a new thread

Post by SalmonsSteve »

I have written an application that is essentially a project manager allowing you to collect a number of different file types into a wxTreeCtrl. Code has also been written using wxWidgets to create the gui for the applications that open each of the file types. The applications function as expected if they are built as an executable and run independently of the project manager. By "as expected" I mean I can open different files in separate instances of the application and each of the controls populates with the correct data. If I launch the application from within the project manager it will function correctly if only one instance exists. If I open two instances and then open a file in the second instance the controls in the first instance populate with the file data while the second instance remains in its initial state.

I think this behavior occurs due to using wxWindow::FindWindowById() to get the controls and push data to them though I'm not sure if it would be different if I was using pointers to the various controls as that would, at this point, require many hours to re-work. My thought is that if I launch each instance of an application, from within the project manager application, in its own thread this wouldn't be an issue. Can anybody confirm if the behavior I am seeking would be possible by doing that? Also I am horribly inexperienced with writing any code that deals with threads, and by that I mean that I have literally never done it. That leads me to the question of how would one do this? Currently the application frame is a class derived from wxFrame which is launched in an event handler (ribbonbarbutton) using the new operator to create a pointer to the application frame.

Any thoughts on how I need to move forward to get the desired results?

***** Edit *******
I don't need the project manager application to process any events from the other application windows. They function as standalone programs and should have no dependence on the project manager.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: launch second application with a new thread

Post by doublemax »

If I open two instances and then open a file in the second instance the controls in the first instance populate with the file data while the second instance remains in its initial state.
That's very strange, that shouldn't be possible at all.

Maybe this happens when you use dynamic linking for wxWidgets and all applications use the same window "pool". Although i would consider this a huge design flaw if this was the case.

But in that case the solution would also be easy: Use static linking for all applications.
Use the source, Luke!
SalmonsSteve
Knows some wx things
Knows some wx things
Posts: 27
Joined: Sun Jun 24, 2012 6:00 pm

Re: launch second application with a new thread

Post by SalmonsSteve »

I am linking against static libs for wxwidgets and passing the following compiler flags: -static-libgcc -static-libstdc++ -static -m32 (or -m64 depending on the build).
I could see using AUIMDIParentFrame and AUIMDIChildFrame potentially fixing the issue but I can't spawn an MDIChildFrame as a standalone application so I would have to use different code files for the two projects which I was trying to avoid. I guess I could use #ifdef statements wrapping the class definition and the constructors to handle switching between standalone and the suite. I haven't explored this option yet.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: launch second application with a new thread

Post by doublemax »

If I open two instances and then open a file in the second instance the controls in the first instance populate with the file data while the second instance remains in its initial state.
I don't believe this until i see it ;)

Are you sure these are two different executables and not two windows of the same executable?
passing the following compiler flags: -static-libgcc -static-libstdc++ -static -m32 (or -m64 depending on the build).
I don't use MinGW, so i'm not sure, but i think these options determine how to link the CRTs.

With which command line did you build wxWidgets, with SHARED=0 or SHARED=1?
Use the source, Luke!
SalmonsSteve
Knows some wx things
Knows some wx things
Posts: 27
Joined: Sun Jun 24, 2012 6:00 pm

Re: launch second application with a new thread

Post by SalmonsSteve »

With which command line did you build wxWidgets, with SHARED=0 or SHARED=1?
I actually have done both in both 32 and 64 builds with both release and debug. I am linking against libraries in my gcc_lib folder rather than those in the gcc_dll folder. I suppose it couldn't hurt to double check that everything ended up in the appropriate folders since I have 8 different builds... though I do know (since I just checked) that dll files are stored in gcc_dll and there are no dll files in gcc_lib.
You are correct about those compiler flags. Essentially the same as linking static in Visual Studio and not having to distribute the VC Runtime versus not linking statically and having to distribute the runtime.
Anyway, in this project I have build configurations already configured for all 8 of those options so I need to build the other configurations and see if this behavior goes away when I am not linking everything statically or with any of the other configs. I will respond with more info after I have done that.
I don't believe this until i see it ;)

Are you sure these are two different executables and not two windows of the same executable?
I didn't believe it either! I suppose it could be two windows of the same item but I don't think that it should be.

Code: Select all

//header file inside class declaration
...
//frame for application class inherited from wxFrame
appFrameClass* someApp;
...

Code: Select all

//code file inside method
...
// get a new pointer and launch a new frame
someApp = new appFrameClass(.... args.....);
...
That, in a very small nutshell is what I'm doing. That single line in the header and that single line in the implementation are the only places where the application code for the secondary application is referenced.
In the standalone I have a very basic myApp.h and myApp.cpp that implements the app. Those two files have not been brought into the project manager code project but the rest of the secondary application's files are part of the larger project. So, really in the grand scheme of things I think my wording is misleading in that the second application, in this case, is contained within the executable of the first application. The project manager is an AUIParentFrame at this point where the secondary applications are wxFrames.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: launch second application with a new thread

Post by doublemax »

Code: Select all

// get a new pointer and launch a new frame
someApp = new appFrameClass(.... args.....);
Without seeing what appFrameClass does, this looks just like another frame in the same executable.

If you started a new process, i would be something using wxExecute or wxProcess.
Use the source, Luke!
SalmonsSteve
Knows some wx things
Knows some wx things
Posts: 27
Joined: Sun Jun 24, 2012 6:00 pm

Re: launch second application with a new thread

Post by SalmonsSteve »

this looks just like another frame in the same executable
That's exactly what I am doing. I figured that a new instance of the class would run without interferring with other instances of the class. That appears to be an incorrect thought. So is it correct to say that what I would need to do is have the application executable compiled and then launch it with wxProcess to have the application launch from inside the project manager? Also, if the project manager is closed would the other applications close as well? I'd like to be able to clean up all the windows at once rather than leaving applications running if the manager has closed.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: launch second application with a new thread

Post by doublemax »

Ok, things are starting to make sense again :)

I wouldn't go the path with individual processes. I think it'll be much more work in the end than actually fixing your current code.

Using FindWindowById is error prone and i would recommend to refactor your code and get rid of it. But as i don't know your code, i can't estimate how much rework that would actually require.

Alternatively, look at the second parameter to FindWindowById, it takes a wxWindow* that controls from where it starts searching. If you pass the frame instance there, it shouldn't find controls in other instances of this frame.
Use the source, Luke!
SalmonsSteve
Knows some wx things
Knows some wx things
Posts: 27
Joined: Sun Jun 24, 2012 6:00 pm

Re: launch second application with a new thread

Post by SalmonsSteve »

I can't believe I didn't look at the documentation for FindWindowById(), I'm normally pretty good about that stuff. So the easy rework would be to pass "this" to FindWindowById() since all controls exist and calls to FindWindowById() exist within the same class derived from wxFrame. There would probably be substantial rework of existing code to change the way things are being done. Of course easy isn't always right and there are 5 more of these applications to be written so getting it "right" with this one may be worth the rework.
Using FindWindowById is error prone
You have my attention, please elaborate, what kind of errors would be commonly seen when using FindWindowbyId()?
SalmonsSteve
Knows some wx things
Knows some wx things
Posts: 27
Joined: Sun Jun 24, 2012 6:00 pm

Re: launch second application with a new thread

Post by SalmonsSteve »

Doublemax, thank you very much, adding the second parameter "this" to my calls to FindWindowById() fixed the issue and everything now works as expected.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: launch second application with a new thread

Post by doublemax »

That's good to hear.
what kind of errors would be commonly seen when using FindWindowbyId()?
Just the ones you've already encountered. IDs have to be unique for this to work 100% reliably.
Use the source, Luke!
Post Reply