Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create a C# Library that is Loaded into Memory only Once?

Tags:

c#

class

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

like image 455
Omer Avatar asked Dec 31 '10 05:12

Omer


People also ask

How do you create a function in C?

Syntax of function prototypereturnType functionName(type1 argument1, type2 argument2, ...); In the above example, int addNumbers(int a, int b); is the function prototype which provides the following information to the compiler: name of the function is addNumbers() return type of the function is int.

What is C used to create?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is needed to write a basic C?

Aside from a compiler, the only other software requirement is a text editor for writing and saving your C code.

Is C hard to learn?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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:

  • How to implement shared memory in .NET ?

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

like image 60
Rick Sladkey Avatar answered Oct 31 '22 14:10

Rick Sladkey


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

like image 44
Amnon Avatar answered Oct 31 '22 16:10

Amnon