Is a static object the same object in several running applications?

If you have a Windows service and a Windows forms application that uses the same static object, is it the same object in both applications? In other words, if I update an object in a service, will it also be updated in the forms application, if both of them work at the same time?

+6
source share
5 answers

They run on different processes, so they do not share a static object.

Not an exception related to your question, but threads created in one application is a different story. They will share a static variable if it is not marked with ThreadStatic

+8
source

Not. If you do not do something specific to achieve these objects, do not split between different processes.

+2
source

I think that each application runs in its own process. I really doubt that updating a static object in a Windows service has any effect on a static object running as a Windows forms application.

The Windows service runs under the system account, where, as an application for forms, Windows runs under the User account.

As other comments noted, processes run in different memory. Each process has its own address space.

Windows service responds to commands from the Service Control Manager.

These are completely different things.

0
source

The simple answer to this question is that each process has its own static one, so no, it will not be used in conjunction with the service and desktop process.

The tricky part is that there can even be multiple instances of a static element in a single process.

In Java there is one instance of a static object for each ClassLoader that loads the class. I checked the equivalent functionality in C# . I found this question on SO that says that in C# really is something similar to several class loaders (I think it really is in the CLR ), and although I cannot find any specific link to multiple instances of statics I am sure it will be so.

0
source

Simply put, no
static is ' static per AppDomain ' (and you can have multiple domains for each process), so even for one process you can safely assume that your static variables will be β€œsplit” (this is usually true if you do not create new domains manually , for example, see What is AppDomain? ) - for example, web applications usually break "static" singletones, etc.
In other words, you need to use some persistence to be able to share your data between different applications. Or use remote access, WCF to communicate across the boundaries of the application (domain).

0
source

Source: https://habr.com/ru/post/912420/


All Articles