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

check apps conflict



  • is there way to check if 2 identical qt app is running in the same directory?

    for example both of same qt apps are try to modify a single file, thus create a conflict, is there anyway to check this type conflict.



  • hi @s002wjh
    you can use lock file approach


  • Lifetime Qt Champion

    @s002wjh On Linux you can use lsof command to list open files and filter the list by process id.


  • Moderators

    @s002wjh
    Qt has a QLockFile class
    https://doc.qt.io/qt-5/qlockfile.html

    But both Qt apps have to use it, from what I understand.



  • can I lock one of the apps, so if the 2nd apps is running and access certain directory, it output an error



  • you could prevent second app to run by set a same QSharedMemory key to these.
    I had this problem.

    if it can helped, Place this code in the two applications (their main.cpp before app.exec ();):

    auto m_location=new QSharedMemory ();
    m_location->setKey(a_key);
    if (!memoadd->create (sizeof (int) )
    {
      m_location->attach ();
      m_location->detach ();
     if (!m_location->create (sizeof (int))) 
    {
      //exit(0); or what you want
    }
    


  • @Lemat
    or you can use wmic on Windows or pgrep on macOS or linux to getted out running process's and check if an app name is in it.

    QProcess
    


  • @s002wjh said in check apps conflict:

    is there way to check if 2 identical qt app is running in the same directory
    can I lock one of the apps, so if the 2nd apps is running and access certain directory, it output an error

    You're going to have to a bit more precise if you want a precise answer. What exactly does "running in the same directory" & "access certain directory" mean to you?

    And are you saying your solution is allowed to change the code of both Qt apps?

    Some of the proposed solutions here are trying to prevent a second instance of a Qt application from running. But I think you're saying it's OK to run two instances, what you want is only to control simultaneous file/directory access across the processes? Please clarify which kind of solution you are seeking.


Log in to reply