Multiple app instances, and passing parameters

P

Peter Reinhold

Hi All,

I am developing a small utility, and have run into a small challenge.

The util is used for performing operations on a bunch of files, and it
can be triggered from the right-click menu when you click on a file.

My problem now is, if the util is already running, I want to stop a
new instance from spawning, and, at the same time, pass the parameters
from the soon-to-be-despawned instance, to the main instance.

My solution at this moment is to open a socket on LocalHost when the
first instance starts, and then on the following, check if something
is bound in the specific port, and then pass the parameters via TCP.

But, can this be done more elegantly?

The reason i'm asking is that this approach seems very "hacky" to me,
but, on the other hand, it works, and I haven't been able to make it
loose parameters or misbehave in other ways.


/Peter
 
N

Nicholas Paldino [.NET/C# MVP]

Peter,

The general concept is actually very sound, IMO, as this is exactly what
the WindowsFormsApplicationBase class in the Microsoft.VisualBasic namespace
does.

For more information, check out this previous thread:

http://groups.google.com/group/micr...+author:Paldino&rnum=1&hl=en#8d2d86fcc6e071fe

Also, you can check out the documentation for the
WindowsFormsApplicationBase class:

http://msdn2.microsoft.com/en-us/li...tionservices.windowsformsapplicationbase.aspx

Specifically, you want to check the StartupNextInstance event:

http://msdn2.microsoft.com/en-us/li...formsapplicationbase.startupnextinstance.aspx
 
A

Arnshea

Hi All,

I am developing a small utility, and have run into a small challenge.

The util is used for performing operations on a bunch of files, and it
can be triggered from the right-click menu when you click on a file.

My problem now is, if the util is already running, I want to stop a
new instance from spawning, and, at the same time, pass the parameters
from the soon-to-be-despawned instance, to the main instance.

My solution at this moment is to open a socket on LocalHost when the
first instance starts, and then on the following, check if something
is bound in the specific port, and then pass the parameters via TCP.

But, can this be done more elegantly?

The reason i'm asking is that this approach seems very "hacky" to me,
but, on the other hand, it works, and I haven't been able to make it
loose parameters or misbehave in other ways.

/Peter

If you've got an overactive QA person, one who likes to double click
on things as fast as they can to see if anything breaks, you might
want to use a mutex (use an app-specific, unique name). The first
instance would hold the mutex until it exits; anything else would
queue up the request via TCP.

Remoting is another way to handle the interprocess communication but
if the parameters are relatively simple it might be overkill...
 
P

Peter Reinhold

want to use a mutex (use an app-specific, unique name). The first
instance would hold the mutex until it exits; anything else would
queue up the request via TCP.

It might be a good idea to implement a combination of a mutex and then
if that is set, deliver parameters via TCP
Remoting is another way to handle the interprocess communication but
if the parameters are relatively simple it might be overkill...

I actually checked out remoting during my first analysis, and I deemed
it to be overkill for passing a filename between two applications.


/Peter
 
P

Peter Reinhold

The general concept is actually very sound, IMO, as this is exactly what
the WindowsFormsApplicationBase class in the Microsoft.VisualBasic namespace
does.

Ok, thats nice to hear :)

Thanks for the links Nicholas, i'll check them out at once.


/Peter
 
P

Peter Duniho

Peter said:
It might be a good idea to implement a combination of a mutex and then
if that is set, deliver parameters via TCP

I agree. I think that's what Arnshea was suggesting.
I actually checked out remoting during my first analysis, and I deemed
it to be overkill for passing a filename between two applications.

One advantage remoting might have is to avoid the issue of what to do if
the port you've selected for your socket is already in use by something
else. I haven't used it myself, but I assume it uses a communication
mechanism that ensures a connection between the two endpoints can always
be created.

If you already have some mechanism for having the port auto-selected,
but still discoverable by subsequent instances of the application, then
I'd agree remoting is probably overkill. Otherwise, you might consider
it still. :)

Pete
 
S

Stefano Canepa

Hi All,

I am developing a small utility, and have run into a small challenge.

The util is used for performing operations on a bunch of files, and it
can be triggered from the right-click menu when you click on a file.

My problem now is, if the util is already running, I want to stop a
new instance from spawning, and, at the same time, pass the parameters
from the soon-to-be-despawned instance, to the main instance.

My solution at this moment is to open a socket on LocalHost when the
first instance starts, and then on the following, check if something
is bound in the specific port, and then pass the parameters via TCP.

But, can this be done more elegantly?

The reason i'm asking is that this approach seems very "hacky" to me,
but, on the other hand, it works, and I haven't been able to make it
loose parameters or misbehave in other ways.

/Peter

We control that a single instance application is runnig in this way

using System;
using System.Collections.Generic;
using System.Text;

namespace SingleInstanceApp
{
public class CSingleInstanceApp
{
public static bool IsSingleInstance()
{
System.Diagnostics.Process[] aoProcesses =
System.Diagnostics.Process.GetProcessesByName(

System.Diagnostics.Process.GetCurrentProcess().ProcessName);

if(aoProcesses != null && aoProcesses.Length > 1)
return false;
else
return true;
}
}
}

this way I can use this class in other apps.

In the main of your program you can write something like:

if (SingleInstanceApp.CSingleInstanceApp.IsSingleInstance()) {
<main application>
} else
{
<call to a function that uppend orders to a List>
}

Adding a time to the mainform that call a function that process the
order List should work.

Hope this help
Stefano
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top