1

I have two projects A and B. Where project A is a system tray application, and project B is the main application. When the user changes the settings from project B, project A needs to update itself. How can I notify project A with the changes that happen?

I have tried to use Global Events but this needs to create a new instance of app A in app B when it starts.

HmH
  • 389
  • 3
  • 14
  • You haven't give much detail. Are the applications both running on the same machine? Are the settings held in memory or stored in a file on the disk? If it's the latter i.e. stored in a file on disk it would be trivial to write a little bit of code to poll the file and check for changes every x seconds or minutes etc. – Andrew Oct 29 '13 at 14:41
  • 1
    @Andrew, I think it is safe to assume that they are running on the same machine :) – Moo-Juice Oct 29 '13 at 14:42
  • 3
    If the settings are stored on disk, you can use a `FileSystemWatcher` to monitor the file for changes. – Eiríkur Fannar Torfason Oct 29 '13 at 14:43
  • If `A` is written in VB.Net then you can Check the "Make single instance application" in Project --> Properties, then just "run" it again with Process.Start() and a Parameter. The existing instance will receive an Application.StartupNextInstance() event from where you make that instance do something. Whether this is useful depends on what information needs to be passed via command line parameters, or if the new data is in a common location (database?). If you just need to "notify" it do something then you can send a simple string or numeric code. – Idle_Mind Oct 29 '13 at 16:09
  • Both applications are running on the same machine, and the settings are saved in a text file. – HmH Oct 29 '13 at 19:05

2 Answers2

4

There are several options. One way is to create a named EventWaitHandle. When project B updates the settings, it sets the event. Project A has a thread that waits on that event. When it gets a notification, it updates from the settings and then resets the event. I have a simple example here: Send message from one running console app to another

That will work if project B stores the new settings somewhere where project A can read them. If you want to do it in memory (i.e. project B doesn't persist the settings), then you need to set up some type of communication between the the two processes. You can set up shared memory using a MemoryMappedFile, in which case you'll still need the event notification.

Or you could set up a Named Pipe with a server stream in project B and a client stream in project A.

Community
  • 1
  • 1
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • EventWaitHandle is a good solution. About Named Piped I also need a thread that waits for the response or else the app will hang. – HmH Oct 29 '13 at 19:24
  • @HmH: You wouldn't need a thread to wait on the named pipe. You can create the client and do an asynchronous read with `BeginRead` or `ReadAsync`. See documentation for [NamedPipeClientStream](http://msdn.microsoft.com/en-us/library/system.io.pipes.namedpipeclientstream(v=vs.110).aspx). – Jim Mischel Oct 29 '13 at 20:21
0

You could also use something like NancyFX that allows self hosting to expose an HTTP endpoint from each application to allow communication without the need for a physical file.

Check out: https://github.com/NancyFx/Nancy/wiki/Self-Hosting-Nancy

CSharpYouDull
  • 229
  • 1
  • 4