Control multiple program instances - open multiple files problem

1k views Asked by At

This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine.

So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files. My system (ubuntu 10.10), when I right click a file and I choose "Open With 'Default Application'" the it runs

Code:

default_application path/to/the/selected/file1

So, if you select multiple files and select "Open With 'Default Application'" the system will call

Code:

default_application path/to/the/selected/file1
default_application path/to/the/selected/file2
default_application path/to/the/selected/file3

So, this is a big problem for me, because I handle the concurrent processes from inside the program, so when another instance of the program is running, a warning message is appeared. So, each application's call will recognize the others as currently running applications and so it'll show the message. I'll end up with 3 Messages saying that another process of the program is running --_--' My application handles multiple URLs this way:

Code:

myapp path/to/the/selected/file1 path/to/the/selected/file2 path/to/the/selected/file3

How can I make my code handle all these multiple instances at the same time? Everything I've tried fails, because everything I've tried requires a check from the first instance called, which is too slow and other instances come app and all together are warning about concurrent processes of the same program

So, how can I fix this? is it system depended, or can I do something with the code?

1

There are 1 answers

1
neuro On

The way is to make your application recognize that there is already an instance running and make the new instance just forward a request to the first instance before dying :)


EDIT:

The way to do that is to have your first application instance behave as a server. The pseudo algo is something like :

start();

try_to_contact_master_server_instance();
if(no_master())
{
    I_am_master();
    start_listening_server_that_wait_for_requests();
}
else
{
    send_request_to_master("open file path/to/the/selected/file1");
    send_request_to_master("open file path/to/the/selected/file2");
    send_request_to_master("open file path/to/the/selected/file3");
    die();
}

handle_incoming_requests();

I hope it's clearer ? Tell me if you need more precisions ...

For the server part, you can do your own or use some software bus provided by the OS like dbus or whatever, but it makes your application dependent, of course.

my2c