Reputation: 423
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;
}
}
Upvotes: 0
Views: 412
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
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
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