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 the object in the service will it also be updated in the forms application as well if both are running at the same time?
They run on different processes so they don't share the static object.
Not exaclty related with your question but threads created on the same application is a different story. They will share the static variable unless is marked with ThreadStatic
attribute
No. Unless you do something specific to achieve this objects are not shared between different processes.
I think each application runs in its own Process. I really doubt that updating a static object in Windows service have any effect on static object running as Windows forms application.
Windows service runs under system
account where as a Windows forms application runs under User
account.
As others have pointed out in the comments, the processes run in different memory. Each process has its own address space.
Windows service responds to Service control Manager commands.
They are completely two different things.
The simple answer to this is is that each process has its own static so no, it will not be shared between the service and desktop process.
The complicated part is that there may even be multiple instances of a static in a single process.
In Java
there is one instance of the static object for each ClassLoader
that loads the class. I checked for equivalent functionality in C#
. I found this question on SO that suggests that there is indeed something similar to multiple classloaders in in C#
(I guess actually in CLR
) and though I did not find any specific reference to multiple instances of a static I am sure that would be the case.
Simply put no,
static is 'static per AppDomain
' (and you could have multiple domains per process), so not even for one process is safe to assume that your static variables will be 'shared' (normally is true unless you create new domains by hand, e.g. see What is AppDomain?) - e.g. web apps typically break the 'static' singletons etc.
In other words you need to use some sort of persistence to be able to share your data in between different applications. Or use remoting, WCF to communicate over application (domain) boundaries.