how to share a static variable between 2 different process

R

Riccardo

Hi everybody,
I have a c# dll and I want to call the class library from several
applications.
I need some variables to be static in the memory space, not locally to each
process.
Thus, if application A modifies the variable X of a class contained in the
dll, application B should be able to see the changed value.
Does anybody has any idea of how to obtain this?

Thanks in advance.
Riccardo
 
C

Ciaran O''Donnell

This is not possible with normall coding in .NET. You would need to setup
interprocess communication in a client server type way.
Remoting or WCF would be the best mechanisms for establishing but you would
need to work out which process should hold the master copy and then create a
service in it for the other processes to access it. You could potentially
propogate changes around with events/callbacks over the communication channel.

The other option is to hold the values in a database, or the registry or
some other centralised storage mechanism.
If you give a bit more information on the requirement we might be able to
help you choose the best way to do it.

HTH
 
R

Riccardo

This is not a good news for me :)
I'll try to explain better: my dll has been exposed as a COM object thanks
to interop. All the clients could call some methods like ListAvailableSeats,
ReserveSeat, PaySeat, etc. The dll has a full control on the access db where
available seats are stored. The db just has 2 fields: Id, Description. I
cannot change its structure.
The multiple COM clients could be located on different machines, require the
list of the available seats, reserve one of them and pay the reserved one. Of
course one client could pay only seats who reserved itself.
The only operation that the dll could do on the db (except reading it) is to
remove the appropriate record whenever the seat is paid.
What I would like to do is keeping trace (possibly only in memory, not on
disk) of the reservations and the related process who made the reservations.

Thanks.
Riccardo Mingozzi
 
A

Alberto Poblacion

Riccardo said:
This is not a good news for me :)
I'll try to explain better: my dll has been exposed as a COM object thanks
to interop. All the clients could call some methods like
ListAvailableSeats,
ReserveSeat, PaySeat, etc. The dll has a full control on the access db
where
available seats are stored. The db just has 2 fields: Id, Description. I
cannot change its structure.
The multiple COM clients could be located on different machines, require
the
list of the available seats, reserve one of them and pay the reserved one.
Of
course one client could pay only seats who reserved itself.
The only operation that the dll could do on the db (except reading it) is
to
remove the appropriate record whenever the seat is paid.
What I would like to do is keeping trace (possibly only in memory, not on
disk) of the reservations and the related process who made the
reservations.

Go beyond COM: use COM+.
Make the class inside your DLL inherit from
System.EnterpriseServices.ServicedComponent. Then register the DLL in
ComponentServices in a server. If you need to call it from various client
PCs, you can generate a proxy from the Component Services administration
tool, and then install the proxy on the clients. This is not needed if the
programs that call the dll are running on the same computer.
You can configure COM+ to run the DLL in server mode (versus library
mode). You can then use static variables, or use the Shared Property Manager
in COM+, to share your values in memory between all callers.

The complete process is a little more complex than hinted above.
Microsoft official course number 2557 describes how to program COM+ using
..Net.
 
C

Ciaran O''Donnell

Ok, so thats a little different. If you are using COM then you can load the
dll into Component Services in Windows as a COM+ server. There are various
tutorials on the web that will talk you through registering the server and
generating a type library (tlb) file to add as a reference to your client
code.
Architecturally it would be nicer to move away from COM and setup a WCF
service to host the server and have that access a SQL Server (SQLEXPRESS is
free) database but if you really cant change that then dont worry about it.

Here is a page which gives a walkthrough of building a COM+ server
application which is implemented in .NET

Hope this helps
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top