Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

multithreading - sharing variables between running applications in C#

I am developing in C# two simple applications, running in the same local machine without network requirements.

The first application initializes an DLL (Class1) and set a variable. The second application just read it the data which was previously stored. Both applications instanciates the same Class1.

Code:

  • DLL (Class1):

    public class Class1
    {
    
    private string variableName;
    
    public string MyProperty
     {
        get { return variableName; }
        set { variableName = value; }
      }
    
    }
    
  • Application A:

    class Program
    {
    static void Main(string[] args)
    {
        Class1 class1 = new Class1();
    
        string localReadVariable = Console.ReadLine();
    
        class1.MyProperty = localReadVariable;
    
       }
    }
    
  • Application B:

    class Program
    {
        static void Main(string[] args)
    {
        ClassLibraryA.Class1 localClass = new ClassLibraryA.Class1();
    
        string z = localClass.MyProperty;
    
        Console.WriteLine(z);
    }
    }
    

My problem is that I do not know how to read a variable from another thread.

Application B must read the "variableName" set by application B

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need some sort of mechanism to communicate between the applications.

This can be through the registry, files, memory mapped files etc...

If both applications are expected to do write, you need to add synchronization logic to your code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...