Daniel Camacho
Daniel Camacho

Reputation: 423

How to get a static property declared from another instance. c#

My C# application is executed and set a variable static "_user". Afterwords another application is executed under the same process and it must read that variable. I cannot obtain the expected results.

Upvotes: 0

Views: 412

Answers (3)

phoog
phoog

Reputation: 43056

Static data only lives as long as the application domain (AppDomain). When the AppDomain is unloaded, its memory is released, and any data stored in that memory is lost.

If, in your Main method, you first call LoginDialog.RunDialog(), you should get the expected result.

If you really need the login to run in a separate AppDomain, you'll need to persist some data to a well-known location on the disk, or use some other method of inter-process communication.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503459

I suspect that whatever's hosting your applications is creating a new AppDomain for each application. That segregates them from each other pretty much as if they were in different processes.

I suggest you save the results to disk, rather than trying to use static variables.

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174457

That's impossible, because each process has its own address space and thus its own instance of LoginDialog.User. You need to use some kind of inter process communication like Shared Memory or Named Pipes.

BTW: Starting one application from the other will not lead to one process that executes both applications. Each application has its own process.

Upvotes: 3

Related Questions