Single instance application using remoting errors

  • Thread starter Thread starter Legend UK
  • Start date Start date
L

Legend UK

Hi,

I am experiencing an issue with remoting trying to implement a single
instance app behaviour required for our application.

I am using the MS best practice recommended solution of using Remoting and
Mutexes to provide this behaviour. In simple terms this is the process:

1. The application's Main() method creates an instance of a
SingleInstanceHandeler class (code attached).
2. The Main() method then calls the Run method on this class.
3. The Run method creates a unique key for this application (user and
workstation specific also) and attempts to lock a Mutex for this key. If it
succeeds this is the first instance. If it fails it is not the first
instance.
4. If this is the first instance a remotable version of the
SingleInstanceHandeler is registered and a channel created to listen for
requests. The OnStartup event is then fired, passing the command line
arguments, which is handled by the class in which the application's Main
method is contained.
5. If this is a second instance, a connection to the remoted version of
SingleInstanceHandeler is created and the OnStartUp event fired on the
"remote" object passing the command line arguments. The second instance then
exits.

Now this works great for a short period of time. However, after some time
(maybe 5 minutes) the remoting channel stops "listening" (my best guess as
to the problem). This means that any second instance fails to create a
connection to it and throws an exception.

I have also tried using the MS implementation of this logic as available in
this
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/reaworapps1.asp)
MS sample. But it appears to suffer from a different issue throwing an
exception from within mscore.dll which implies a framework bug.

Here is the code used for the Main() method:

[STAThread]
static void Main(string[] args)
{
SingleInstanceHandler InstanceHandler = new SingleInstanceHandler();
InstanceHandler.StartUp +=new
StartUpEventHandler(InstanceHandler_StartUp);
if(InstanceHandler.Run(args))
{
// Intialise the tool bar form but never show the form.
// The task bar icon will show however.
frm_Common_TaskbarIcon frm = new frm_Common_TaskbarIcon();
Application.Run();
}
}

The code for the SingleInstanceHandler class is attached.

I would be grateful for any help anyone can provide on this. I am sure I am
just missing something obvious with regard to the life span of the remotable
object. But can't seem to see what.

Thank you in advance for your help.

Regards,

Anne.
anne.catterick @ no-spam.legendware.co.uk
 
Hi

In .NET remoting, we have build-in mechanism to support singleton.
Here is link to the sample you may have a try.

NET Samples - How To: Remoting
Singleton Sample
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpqstart/ht
ml/cpsmpnetsamples-howtoremoting.asp

Do you have any concern when you use the build-in singleton in .NET
remoting?


Best regards,

Perter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top