I have a Windows Service that performs a number of periodic activities, and I want to change the settings of this service from a Windows Forms app. I\'m not sure, though, ab
Assuming that everything is running on the same machine, how about this:
Define a common c# structure that defines the settings. All projects include this .cs file. Define this class as a struct with a StructLayout of Sequential or Explicit so it can be mapped directly into unmanaged shared memory. For example:
[StructLayout(LayoutKind.Sequential)] unsafe struct MySharedSettings { public int setting1; public int setting2; public string setting3; // add more fields here. }
Use named shared memory (aka: memory-mapped files). This allows multiple processes on the same computer to share data, without the overhead of Remoting or WCF. Shared memory is extremely fast and, unlike pipes, offers random access to the shared memory data. The service would create the named shared memory, and the UI applications would open the shared memory. You would have to use pinvoke to use the underlying Windows APIs, but this is not a big deal.
The UI applications write the MySharedSettings to the shared memory, while the service reads the shared memory.
Use a named Semaphore and/or named Mutex to protect access to the shared memory and to signal the availability of new settings. The service has a dedicated background thread that simply perform a WaitOne() on the semaphore, and the UI thread will signal when new data is written.