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

Daniel Camacho
  • 423
  • 5
  • 12
  • 27
  • This might be what you're looking for http://stackoverflow.com/questions/1360533/how-to-share-data-between-different-threads-in-c-sharp-using-aop – Wim Ombelets Mar 21 '12 at 11:16
  • I hope you are aware of the fact that each program has its own instance of `Class1`! – Daniel Hilgarth Mar 21 '12 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. – Daniel Camacho Mar 21 '12 at 11:25

4 Answers4

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 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? – Daniel Camacho Mar 21 '12 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 '12 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: http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication – Slugart Mar 21 '12 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. – Daniel Camacho Mar 21 '12 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 '12 at 12:49
  • @Slugart - No, but even in proc there is quite a large overhead, in particular for such a small amount of data. – Oded Mar 21 '12 at 12:52
  • @Slugart - Added CPU and memory usage during runtime. – Oded Mar 21 '12 at 13:01
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..

Community
  • 1
  • 1
Skizz
  • 69,698
  • 10
  • 71
  • 108
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.

Ed Power
  • 8,310
  • 3
  • 36
  • 42
0

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

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
  • 2
    Remoting has been deprecated for ages now. WCF has been the mechanism to use for quite a while now. – Oded Mar 21 '12 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... – Christoph Fink Mar 21 '12 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. – Brian Gideon Mar 21 '12 at 19:13