Exchange variables between running applications in C #

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

The first application initializes the DLL (Class1) and sets the variable. The second application just read the data that was previously saved. Both applications install the same class1.

code:

  • DLL (Class1):

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

     class Program { static void Main(string[] args) { Class1 class1 = new Class1(); string localReadVariable = Console.ReadLine(); class1.MyProperty = localReadVariable; } } 
  • Appendix 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 should read the "variableName" set by application B

thanks

+3
source share
4 answers

Some kind of mechanism is needed for interaction between applications.

This can be through the registry, files, files with memory mapping , etc.

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

+4
source

There is no easy way for application B to read the data created in application A. Each application has its own address space and, therefore, is not aware of the existence of others.

But there are ways to do it!

See this question for one method.

+1
source

I have successfully used two methods:

  • Use the database table to contain your shared data. If you wrap calls to it in a transaction, then you are also concurrency.

  • Use PersistentDictionary to store your mutex protected data. You should have some interprocess lock since PersistentDictionary can only be opened by one process at a time.

+1
source

You can use .net Remoting to communicate between your two applications.
To delete, a network address for communication is also not required.

0
source

Source: https://habr.com/ru/post/1244889/


All Articles