Question about instance! With or without Frame!

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
Nick
Earned some good credits
Earned some good credits
Posts: 107
Joined: Thu Apr 11, 2019 12:50 am

Question about instance! With or without Frame!

Post by Nick »

I've created 2 exactly the same examples!
1 has Frame and the other just a MessageBox

The Problem is in the example that has no frame! When it is executed it is showing a message on the screen that doesn't even exist in the program, and I don't know why!

Example with frame that works!

Code: Select all

#include <wx/app.h>
#include <wx/frame.h>
#include <wx/msgdlg.h>
#include <wx/snglinst.h>

class MyProgram: public wxApp {
public:
   wxSingleInstanceChecker *m_checker = new wxSingleInstanceChecker;

   bool OnInit() {
      if (m_checker->IsAnotherRunning()) {
         wxLogError(_("Another program instance is already running, aborting."));
         delete m_checker;
         m_checker = NULL;
         return false;
      }
      (new wxFrame(NULL, wxID_ANY, ""))->Show();
      return true;
   }
   virtual int OnExit() {
      delete m_checker;
      m_checker = NULL;
      return 0;
   }
};

wxIMPLEMENT_APP(MyProgram);
Example without frame that strangely shows this message when loaded:
Deleted stale lock file '/home/nick/Test2-nick'.

Code: Select all

#include <wx/app.h>
#include <wx/msgdlg.h>
#include <wx/snglinst.h>

class MyProgram: public wxApp {
public:
   wxSingleInstanceChecker *m_checker = new wxSingleInstanceChecker;

   bool OnInit() {
      if (m_checker->IsAnotherRunning()) {
         wxLogError(_("Another program instance is already running, aborting."));
         delete m_checker; 
         m_checker = NULL;
         return false;
      }
      wxMessageBox("Hello");
      return false;
   }
   virtual int OnExit() {
      delete m_checker;
      m_checker = NULL;
      return 0;
   }
};

wxIMPLEMENT_APP(MyProgram);
I'd like to understand why this strange message is appearing if the programs are the same? Whether or not it's your only difference to have a Frame
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

Re: Question about instance! With or without Frame!

Post by Kvaz1r »

You also have difference in return value. Change to true and let's see if it change something.
Nick
Earned some good credits
Earned some good credits
Posts: 107
Joined: Thu Apr 11, 2019 12:50 am

Re: Question about instance! With or without Frame!

Post by Nick »

Kvaz1r wrote: Wed Apr 15, 2020 7:54 pm You also have difference in return value. Change to true and let's see if it change something.
Good attention, but that's not it! I just tested it!
The reason for the return false is to close the application after reading the msg, since there is no way to close the application otherwise.

The message appears to be created when this line is checked:
if (m_checker->IsAnotherRunning()) {

Since if I put a messagebox before the line it runs first

But remembering that it is not executed within the if condition. And yes when testing the if condition

Giving me the impression that this command is what generates the msg When I don't use a wxFrame
IsAnotherRunning()
Nick
Earned some good credits
Earned some good credits
Posts: 107
Joined: Thu Apr 11, 2019 12:50 am

Re: Question about instance! With or without Frame!

Post by Nick »

I was disappointed with this instance-checking solution! https://docs.wxwidgets.org/3.1.3/classw ... ecker.html

What it actually does in an extremely complicated way is to create a file in my Hd to verify if this file exists or not.
The annoying message he gives is precisely to inform you that he deleted the file!

I can verify instance using the same idea in a much simpler and easier way!

The only thing that served me was to give an idea, but for that I do not need any way to use wxSingleInstanceChecker. It just doesn't make sense to use.
It seems rather odd this solution presented. But that's okay, at least I liked the idea it represents to do it my way!
Post Reply