Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Standalone app that can be run only once at a time



  • Hi,

    I almost have no experience of creating standalone executable file.

    There are two types of applications:

    1. one can run many times at the same time (each double click on executable file is going to execute new window with this app)
    2. one can run only once at a time. Another double click will produce an error that application is already running

    By default how the standalone application work? Does it launch new window with each double click or it can be run only once at a time?



  • Hi,

    I have created a couple of applications in QML that run on my Windows 7 64bit laptop. Certainly in my case, each time the .exe is double clicked, another instance is opened.

    I don't having any idea how to create a 'run once' application.

    A quick search revealed this; https://forum.qt.io/topic/83317/qt5-prevent-multiple-instances-of-application/ it might be of help, I don't really know, if it is, then your post is a duplicate :)



  • Hi,

    I have created a couple of applications in QML that run on my Windows 7 64bit laptop. Certainly in my case, each time the .exe is double clicked, another instance is opened.

    I don't having any idea how to create a 'run once' application.

    A quick search revealed this; https://forum.qt.io/topic/83317/qt5-prevent-multiple-instances-of-application/ it might be of help, I don't really know, if it is, then your post is a duplicate :)



  • @Markkyboy Thank you for sharing your experience and for the link
    That is totally what I wanted
    I couldn't formulate my question nicer than I did so I didn't find this link by myself :(



  • @Please_Help_me_D - You're welcome :)



  • @Markkyboy there is dilemma that I cannot solve.
    I do want that my application to be available in few window (launched many times) but sinse my app allow user to create projects then I have to know project name of every launched MyApplication.
    For example user launches MyApp and opens C:/project_1.prj
    After that user launches MyApp and opens C:/project_2.prj

    In each instance of MyApp launched I need to know what project is chosen.
    I use SqLite and QSettings but I don't see straightforward way to store such information. Maybe global variables is useful in this sense?
    P.S. I didn't use global yet at all :)



  • Imo, to be able to restrict the executable to be run only once, you should check the system's running progress table on your executable's startup to see if there is a progress running with your executable's name or some other information, if exists, exit, if not, run along.

    It's simple to make a two mainwindow appear, one created in main, the others created inside the existed mainwindow instance, the tricky part is to manage the shared data.

    With two executable running, it's two progress, and ipc (Inter-Process Communication) is very restricted compared to multi-thread solution.



  • @Gerald-L thank you for sharing opinion
    But the main question for me now is how to understand wich launched application manages wich project. If there are two running app and each one has its own project (C:/project_1.prj and C:/project_2.prj) where and how to store the name of the current project for each app?



  • @Please_Help_me_D
    There are many possible approached. Earlier you referenced https://forum.qt.io/topic/83317/qt5-prevent-multiple-instances-of-application/. From there, you could use QSharedMemory among your instances as your way of recording which current instance is working on which project, and take it from there....


  • Lifetime Qt Champion

    Hi,

    Just in case, QtSingleApplication works seamlessly across plateforme taking in charge the backend stuff needed.



  • @JonB thank you
    I just read about shared memory

    How do you think if I solve my problem in the following way:

    1. create shared memory where I keep QList of app's instances id. When app is launched it checks how many apps are already run: if it is empty then write shared memory value 1 else add 1 to the last QList value and this will be id for this instance.
    2. define global variable which stores id of the instance
    3. define global variable wich stores project name chosen in current instance

    Is this approach going to work?
    Does global var works only for current instance or they are visible inside all the instances?


  • Lifetime Qt Champion

    No, global variables are not shared between instances of an application.



  • @SGaist hi,
    How do you think should I use QtSingleApplication or approach based on a shared memory and global vars? Gloabl is used with extern modifier
    Does QtSingleApplication fits my needs?


  • Lifetime Qt Champion

    As already said: global variables are not shared between instances.

    Well, you wrote that you want to run one application instance only. That's what QtSingleApplication is designed for.



  • @SGaist
    It seems to me the question has now changed.

    @Please_Help_me_D said in Standalone app that can be run only once at a time:

    But the main question for me now is how to understand wich launched application manages wich project. If there are two running app and each one has its own project (C:/project_1.prj and C:/project_2.prj) where and how to store the name of the current project for each app?

    The OP now wants to run multiple instances of his application, and be able to read/write what "project" each is on. I was thinking each could store this information in QSharedMemory. I guess you could also use QtSingleApplication::sendMessage() to exchange that information, though that class seems more aimed at ensuring only one instance is running?



  • @SGaist I confused you I'm sorry

    I think I found a solution but just in case that it will be useful for somebody (I'm in doubt but still..) I rewrite the problem and the solution:

    The problem
    MyApp should be available to run in few instances. Each instance should work with its own project (bunch of files). If the first instance works with project_1 then this project shouldn't be available for other instances.

    The solution
    To keep information which instance works with wich project I introduce global var QString PRJ_NAME; with extern modifier
    To store list of opened projects I create QList <QString> in shared memory and store there PRJ_NAME of each instance

    I think this should work



  • @JonB yes, the problem changed and I'm ashamed of that.. I'm sorry for confusing

    But I think to avoid using QtSingleApplication (I don't feel that I need to run local server to communicate between instances) and simply do the above method based on Shared Memory



  • @Please_Help_me_D
    Your post has crossed with mine. Yes, I thought that is what you want now. The principle you outline is what I had in mind.


Log in to reply