tags:

views:

380

answers:

3

I have two executables that reference the same Class Library. In the class library I have a static variable. How can that static variable persists on the two different executables?

This is how it looks:

public class MyClass
{
    public static string MyVar;
}

App 1:

public class MyApp1
{
      public void SomeMethod()
      {
           MyClass.MyVar = "hello";
      }       
}

App 2:

public class MyApp2
{
      public void SomeOtherMethod()
      {
           if(MyClass.MyVar == "hello")    
                DoSomething();
      }       
}
+6  A: 

There's nothing built-in to do this. Would you want the static variables to be persistent across invocations of the executable as well, or just while both were running at the same time? Basically you're looking at "normal" persistence mechanisms (and thinking about liveness - detecting when one process needs to reload its state).

I would personally try to design around this to avoid even wanting to do it. Consider having a separate service which both apps talk to instead.

Jon Skeet
+2  A: 

The only way to share data betwin appdomains is remoting (WCF, .net remoting or etc.)

ArsenMkrt
+3  A: 

This will sound stupid.
But write it to a text file on a common location & read from it when needed.

shahkalpesh
not as stupid as you think!!! ;)
dboarman