Michael Richardson
Michael Richardson

Reputation: 4282

Can a .NET ApplicationDomain contain more than one process?

In .NET, is there any way that a single static class member can be accessed by more than one process? I've read that static members are scoped to an ApplicationDomain and also that processes can contain multiple ApplicationDomains, however I've been unable to confirm that an ApplicationDomain is absolutely confined to a single process.

I've always understood/believed that processes are highly segregated by the operating system. Also, in the simple testing that I've done (using Process.Create()) it appears that static members in different processes are entirely independent. However this (extremely helpful) answer that I received on CodeReview.SE makes me wonder whether bleed-over is possible.

Update: To be clear, I have no desire to access a static member from multiple processes. I'm purely trying to determine if I need to code against this possibility. Please see the linked question if you are interested in the background to this question.

Upvotes: 3

Views: 118

Answers (2)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93444

No, an AppDomain cannot run under more than one process. AppDomains were created as a way to allow a single process to securely segregate multiple applications within the same process. So, by definition, an AppDomain cannot exist in more than one process.

As for sharing memory, however... there are a number of ways to share memory between processes. In general, in .NET if you make something static, it will not be shared between processes (or app domains). But, through native code, you can have shared memory pages through a number of mechanisms. Hans already mentioned Memory Mapped Files. I disagree that they're difficult to get right. I use them now and again and have never had much issue.

Another method is shared data and code segments. If you run the same application several times, any shared segments can be shared between application instances. This is not so easy to do in .NET, and would likely require massaging the final PE format to make it work.

Upvotes: 2

usr
usr

Reputation: 171178

Static variables belong to a single appdomain. Processes never share static variables. You do not need to code against it.

By default, processes are isolated. You have to actively work to share memory between two processes. The CLR makes no attempt to communicate with other processes.

Upvotes: 2

Related Questions