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.
-
Application 1: Setting a _user variable:
public class Program { public static void Main(string[] args) { LoginDialog login = new LoginDialog(); login.RunDialog(); } } -
Class called by Application which set the variable _User
public class LoginDialog { private static string _user; public void RunDialog() { _user = "Peter"; } public static string User { get { return _user; } } } -
Application 2: Get static variable declared:
public class Program { public static void Main(string[] args) { string s = LoginDialog.User; } }
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.