Sharing events with multiple programs
-
I am trying to write a DLL that has an event and am subscribing to the event from 2 different programs, but each of my programs is creating a new instance of the DLL. The class is a singleton class and looks like this:
public sealed class clsEvents
{
// Create a new Singleton Instance of the class
public static readonly clsEvents Instance = new clsEvents();private string _Value = "";
public event EventHandler ValueChanged;public void OnValueChanged(EventArgs e)
{
EventHandler handler = ValueChanged;
if (null != handler)
{
handler(this, e);
}
}public string Value
{
get { return _Value; }
set
{
_Value = value;
OnValueChanged(EventArgs.Empty);
}
}
}The singleton class only does half the work so that the same instance of the class is shared within each program but I need to use the same instance in both programs. I have set both programs to compile into the same directory so I know they are loading the same DLL but they are each creating their own instance of the class. Finally, my question: In VB6 it is possible to set the Instancing of a DLL to Multiuse and any programs that use that DLL would share the same global variables from the DLL. Is it possible to do the same thing in C# so that each of my programs uses the same instance of the singleton class? Forgive me if I am going about this all wrong but hopefully someone can help. Please tell me if there is a better way to share events across multiple programs.
-
I am trying to write a DLL that has an event and am subscribing to the event from 2 different programs, but each of my programs is creating a new instance of the DLL. The class is a singleton class and looks like this:
public sealed class clsEvents
{
// Create a new Singleton Instance of the class
public static readonly clsEvents Instance = new clsEvents();private string _Value = "";
public event EventHandler ValueChanged;public void OnValueChanged(EventArgs e)
{
EventHandler handler = ValueChanged;
if (null != handler)
{
handler(this, e);
}
}public string Value
{
get { return _Value; }
set
{
_Value = value;
OnValueChanged(EventArgs.Empty);
}
}
}The singleton class only does half the work so that the same instance of the class is shared within each program but I need to use the same instance in both programs. I have set both programs to compile into the same directory so I know they are loading the same DLL but they are each creating their own instance of the class. Finally, my question: In VB6 it is possible to set the Instancing of a DLL to Multiuse and any programs that use that DLL would share the same global variables from the DLL. Is it possible to do the same thing in C# so that each of my programs uses the same instance of the singleton class? Forgive me if I am going about this all wrong but hopefully someone can help. Please tell me if there is a better way to share events across multiple programs.
I'm afraid I think you're out of luck with this. Each of your programs runs in its own process and the operating system ensures that processes are independant of one another. Your dll will be loaded twice - once into each process, and even though you have implemented a singleton you ultimately have two of them on your computer. You need to look at inter-process communication, something like named pipes perhaps or you might be able to get away with Mutexs. Hopefully someone here might have done this before.
Regards, Rob Philpott.
-
I am trying to write a DLL that has an event and am subscribing to the event from 2 different programs, but each of my programs is creating a new instance of the DLL. The class is a singleton class and looks like this:
public sealed class clsEvents
{
// Create a new Singleton Instance of the class
public static readonly clsEvents Instance = new clsEvents();private string _Value = "";
public event EventHandler ValueChanged;public void OnValueChanged(EventArgs e)
{
EventHandler handler = ValueChanged;
if (null != handler)
{
handler(this, e);
}
}public string Value
{
get { return _Value; }
set
{
_Value = value;
OnValueChanged(EventArgs.Empty);
}
}
}The singleton class only does half the work so that the same instance of the class is shared within each program but I need to use the same instance in both programs. I have set both programs to compile into the same directory so I know they are loading the same DLL but they are each creating their own instance of the class. Finally, my question: In VB6 it is possible to set the Instancing of a DLL to Multiuse and any programs that use that DLL would share the same global variables from the DLL. Is it possible to do the same thing in C# so that each of my programs uses the same instance of the singleton class? Forgive me if I am going about this all wrong but hopefully someone can help. Please tell me if there is a better way to share events across multiple programs.
I'm not certain of my solution...I'm just trying to throw ideas your way. What about exposing this as a web service and having it influence a state machine on your server? Then, you could just proxy into the service in both apps and whenever you would trigger the event from inside each of the programs, it would actually fire the event in a single location. But again, I might be oversimplifying your specific problem.
-
I'm not certain of my solution...I'm just trying to throw ideas your way. What about exposing this as a web service and having it influence a state machine on your server? Then, you could just proxy into the service in both apps and whenever you would trigger the event from inside each of the programs, it would actually fire the event in a single location. But again, I might be oversimplifying your specific problem.