4

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

3
  • This might be what you're looking for stackoverflow.com/questions/1360533/… Mar 21, 2012 at 11:16
  • I hope you are aware of the fact that each program has its own instance of Class1! Mar 21, 2012 at 11:16
  • Yes!! Thats the problem, both applications create different instances and I dont know how to communicate them in a easy way. I ve heard about WCF, pipes, registy... but I cannot find an esay implementation which I am sure that there is... @Wimbo I found it also, but As I know so far, that post is communication between threads from the same instance is It? In my case I have different threads from different instances. Mar 21, 2012 at 11:25

4 Answers 4

4

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.

7
  • I would not like to Use files because the data stored in the variable is a "password" What about registry? Can you provide an snippet code example for that mechanism? Mar 21, 2012 at 11:19
  • @kmxillo - What makes you think that registry usage is safer than files? You can always encrypt the password before writing.
    – Oded
    Mar 21, 2012 at 11:21
  • I would suggest using WCF for inter process communication as you won't have to worry about accessing files nor locking them: switchonthecode.com/tutorials/…
    – Slugart
    Mar 21, 2012 at 11:38
  • 1
    @Slugart WCF overheads a lot the code.. as you see the purpose is just one variable. I do not need the reason why I have to create a communication across the web when the programs are running locally. Mar 21, 2012 at 12:40
  • WCF does not necessarily use 'the web', your local network or even TCP/IP. You should use the NetNamedPipeBinding and not the HttpBinding.
    – Slugart
    Mar 21, 2012 at 12:49
1

There is no simple way for Application B to read data created in Application A. Each application has its own address space and thus do not know of the others existence.

But, there are ways to do this!

See this question for one method..

2
  • What happen when the apps run on the same process? Mar 23, 2012 at 16:22
  • @kmxillo: Each app runs in its own process, they don't share processes.
    – Skizz
    Mar 26, 2012 at 10:23
1

I've successfully used two methods:

  1. Use a database table to contain your common data. If you wrap your calls to it in transactions then you also protection from concurrency issues.

  2. Use PersistentDictionary to store your data, protected by a mutex. You must have some interprocess locking since PersistentDictionary can only be open by one process at a time.

0

You can use .net Remoting to communicate between your two application.
Remoting also does not require a network address to communicate.

3
  • 2
    Remoting has been deprecated for ages now. WCF has been the mechanism to use for quite a while now.
    – Oded
    Mar 21, 2012 at 11:20
  • Thanks for the update - haven't used remoting since 2.0 and actually used WCF, but didn't know its also the recommended way for IPC now... Mar 21, 2012 at 11:54
  • 2
    .NET Remoting may be deprecated, but that does not make this answer technically incorrect or anything like that. It is a legit option; just not the best one. Mar 21, 2012 at 19:13

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.