Question

I have 3 projects in a solution like This:

  1. Xna Game Project (GAME) with reference to Library
  2. Library Project (Library)
  3. Windows Form Project (WIN) with reference to Library

This project have a multiple project Start up (WIN and GAME).

So, in my Library I have a facade to inject the dependencies between them using a singleton to provide an unified way to use a device plugged into a serial connector.

That's why I'm using a singleton... cause I can not have 2 connections to the same port.

In my other 2 libraries (WIN and GAME) I use the navigator through this facade. But the result was always 2 attempts to instantiate the serial port, so the second one always fails!..

Debugging it, adding a breakpoint in that line:

private static readonly LogicalFaccade _instance = new LogicalFaccade();

I requested 2 different calls... I thing one from each project (WIN and GAME).

How can I share an unique instance between projects ?

Is something wrong in my code?...

public class LogicalFaccade
{
    private static readonly object thredLock = new Object();

    private static readonly LogicalFaccade _instance = new LogicalFaccade();
    public static LogicalFaccade Instance
    {
        get
        {
            return _instance;
        }
    }

    private INavigator _navigator;
    public INavigator Navigator
    {
        get
        {
            lock (thredLock)
            {
                if (_navigator == null)
                {
                    _navigator = new SerialNavigator("COM4", 19200, 8);
                }
                return _navigator;
            }
        }
    }
}
Was it helpful?

Solution

By the implementation you have, you will have a singleton object throw many threads of a single process(or EXE).
When having 3 start-up projects you will have 3 different processes, so using singleton pattern (in the way you have implemented) will not help you.

If your 3 projects(processes) will run on a single workstation (PC) you can share the resources by using locking mechanism of Semaphore, a useful sample could be found here.
If they will be distributed on multiple PCs you need to use inter process communications.

Some of major options in .Net are:
WCF
Remoting
ASP.Net WebSevices
WM_COPYDATA
Socket programing

OTHER TIPS

You can achieve it with IPC . Here is link for named pipes example http://www.codeproject.com/Articles/7176/Inter-Process-Communication-in-NET-Using-Named-Pip . hope it be helpfull

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top