6

I would like to create a Class Library DLL in C#, that will have a Static Class in it. That static class has 1 private static member(int), with a public static property for it.

What I want is, that for Every C# Application that references this DLL, it will get the same static class.

Meaning If Application1 changes the static member's value to be 5, and then Application2 tries to get the value of the propery, it will get: 5.

Despite the fact that those are two different applications(EXEs).

In simply words, I want this whole class library to be "static", so only once will be loaded of it to memory, and then its single value will be shared accross different EXEs that reference it.

Thank you

Omer
  • 61
  • 2
  • You have to save data somewhere anyway. So you might want to consider windows registry or isolated storage to store your data. – default locale Dec 31 '10 at 05:15
  • It's called "server". It is an exe. Write the dll to talk to the server. – Hans Passant Dec 31 '10 at 05:39
  • It would probably be more worthwhile if you could describe the initial problem you had/have that led you to this solution. In your comments to chibacity's answer, you indicated that you wanted to share objects (not just simple scalars like integers). This really isn't going to work well. – Damien_The_Unbeliever Dec 31 '10 at 08:11

3 Answers3

3

An attractive solution to shared data amongst all processes on the same computer is shared memory. You will have to rewrite your properties to retrieve the shared value, and the class will be loaded multiple times into each process that uses your library, but it will behave as though there were only one copy, if you do it correctly.

Here is a StackOverflow question to help you get started:

It links to a complete shared-memory library you can use.

Community
  • 1
  • 1
Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
2

What you're looking for is some form of IPC or RPC. One option is .NET Remoting.

Amnon
  • 7,652
  • 2
  • 26
  • 34
0

I am Omer, I registered so it looks as if I am a different user.

Regarding what Damien_The_Unbeliever said, allow me all to describe what I generally want to do, so you can direct me towards the best solution for that case.

I have created a nice application. This application has a GUI. I want to add another way to operate this application, which will be via exposing an API for it - methods and properties that other applications can use, to operate my application.

And what I plan to do is: Create a DLL with a static class, put the core of my application in that class, and then have the GUI, and potentially all other applications, use that class.

please note that this class will hold not only data that can be stored, but also references to "live" objects(i.e. open coomunication ports, etc etc), so storing to disk, and reading from it will not be a good solution here. I need this 1 static class, to be accessible from all applications that want, and that all of them will get the same static class.

So, I need this class to be static, and I need the library to be "static". (no such thing in .NET as "static library", I know. please note that I am not talking about static library like in C++ - there it's something else.. I am talking about a DLL that is created once only, for all applications who use it).

Omer
  • 91
  • 2
  • 9
  • Is this an answer to your own question? If not, and it is just a comment, you should modify your original question with this new info. – Dykam Dec 31 '10 at 12:07
  • I cannot modify my original question because when I registered, it gave me a new user.. so the original question is not associated with me(the new user), so I am not able to Edit it. how can I associate it now? or is it lost? – Omer Dec 31 '10 at 12:36
  • You can't have one static "instance" of a dll shared across multiple processes in Windows. You will need to use inter-process communication or shared memory (see other answers) if you want to communicate between processes. – Jason Williams Dec 31 '10 at 15:19