2

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;
            }
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • This is like to manage the state object across multiple application. So this can be achieve using state server. That means you need to manage a service to get the state of this singleton object. – Sandeep Kumar May 11 '13 at 16:31
  • 1
    if win and game two different program you should use some IPC techniques . pipe,socket for example . – qwr May 11 '13 at 16:46

2 Answers2

2

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

Mohsen Heydari
  • 7,256
  • 4
  • 31
  • 46
1

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

qwr
  • 3,660
  • 17
  • 29