0

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;
        }
    }
    
9
  • 2
    What do you mean by "under the same process"? Commented Mar 27, 2012 at 14:14
  • What error or unexpected behavior are you getting? Commented Mar 27, 2012 at 14:16
  • Both applications are running under an specific external application. (In that software I runn my own applications which are DLL.) I have check that they are in the same process because they have the same pID. Commented Mar 27, 2012 at 14:18
  • @phoog string s = LoginDialog.User; I obtain string s is null. when I declare private static string _user="Peter", it works perfectly, but that is not my aim. So I am pretty sure that I can find a solutions which solves my "small" problem Commented Mar 27, 2012 at 14:20
  • 1
    @kmxillo Please show the code you use to launch these applications "under" another application. Commented Mar 27, 2012 at 14:23

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

The point is: when I declare the static variable like: private static string _user="Peter"; It works perfect, but I cannot declare it like that.
@kmxillo: Define "it works perfect." It sounds like you're either not describing it correctly, or you're not perceiving the results correctly.
@David it "works perfect" because the static variable is initialized. In the sample code in the question, the initialization is contained in a method that is never called.
@kmxillo according to your edited question, RunDialog is called in a separate program. The method is never called in the program that reads the User property.
1

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.

7 Comments

when I am checking with the following statement "System.Diagnostics.Process.GetProcessesByName("").Id." I am getting the same ID. Does it mean that are in the same process? because if it is true, I dont understand the meaning of use Inter-Process communication methods.
@kmxillo it sounds like you're not running the programs at the same time. If that's the case, they will not be in the same process.
@kmxillo it's also possible that the launching application is creating separate application domains for each application, in which case they'd have the same process ID but they would still be effectively separate. If that's the case, substitue "AppDomain" for "process" in all of my comments. I've edited the answer as well.
is there a statement to check whether the applications work in different application domains?
@kmxillo AppDomain.CurrentDomain.Id "Gets an integer that uniquely identifies the application domain within the process."
|
1

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.

2 Comments

I used static variables because data is saved in the "structure" If I saved the results to the disk, not in static variables. Would it be a good practice when data is personal data like "user,password"? If not, Which is the best solution? I dont rather prefer to use DB(there is no need) or RegistryKeys(I dont wanna overhead my code.).
@kmxillo: There are any number of ways of storing data. For a simple list of strings, you could just use "one line per value". Alternatively, XML is pretty easy with LINQ to XML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.