Do instances of the same application share static objects?

Asked

Viewed 49 times

3

I am developing a Windows Form application in C#, and this application can run simultaneously several times on the same computer.

And I am doubtful whether or not I should use static objects (classes, functions, variables, etc.), as they may receive different values at program startup.

Then I wonder if the various instances of the same program will share the same information from the static variable?

Example:

  • There is a static variable To;
  • When the first instance is opened it receives 1
  • When the second instance is opened it receives 2

In the first instance the variable To will continue to be 1?

  • 1

    No. Each instance of the application is a different Appdomain and each Appdomain will have its own static variables.

1 answer

1


No, it never happens, an instance is a separate process, and in fact one is aware that there is another, it is totally separate and there is no sharing between any parts of memory, no matter if it is static or not. So yes, it will still be worth the same amount. You can see more on There is difference between Program, Thread and Process?.

Unless you use a specific mechanism for this, such as Memory Mapped File, and only what is placed inside it can be seen by another instance that opens the same file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.