Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use events to signal between applications?

I have a windows service which updates some reporting information.

I have some other applications running on the same machine which can do things that should cause the reporting information to be updated.

Neither party knows anything about the other, but I want to be able to have the applications do something to signal that the update should occur.

My initial idea was for the applications to raise some sort of system-wide event. They don't know or care if some other service is listening for it to handle it.

Meanwhile, the service is listening for that event and doesn't know or care who raised it.

I can accomplish this with the applications writing to the system event log, and the service listening for that log event.... but this seems like it's cluttering up the application event log.

Is there another, more appropriate, way to handle this?

like image 656
Matthew Avatar asked Feb 11 '14 22:02

Matthew


2 Answers

From the application which sending messages, you could issue an UDP broadcast limited on the local machine, and make the windows service listening to that port.

In this way, the application doesn't need to know the windows service, and vice versa. They are both independent, and could be deployed in its own way.

like image 153
ssett Avatar answered Nov 03 '22 14:11

ssett


You can use memory-mapped files to signal the reporting process. The below example is extracted from MSDN

The first process:

class Program
{
    // Process A: 
    static void Main(string[] args)
    {
        using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 10000))
        {
            bool mutexCreated;
            Mutex mutex = new Mutex(true, "testmapmutex", out mutexCreated);
            using (MemoryMappedViewStream stream = mmf.CreateViewStream())
            {
                BinaryWriter writer = new BinaryWriter(stream);
                writer.Write(1);
            }
            mutex.ReleaseMutex();

            Console.WriteLine("Start Process B and press ENTER to continue.");
            Console.ReadLine();

            Console.WriteLine("Start Process C and press ENTER to continue.");
            Console.ReadLine();

            mutex.WaitOne();
            using (MemoryMappedViewStream stream = mmf.CreateViewStream())
            {
                BinaryReader reader = new BinaryReader(stream);
                Console.WriteLine("Process A says: {0}", reader.ReadBoolean());
                Console.WriteLine("Process B says: {0}", reader.ReadBoolean());
                Console.WriteLine("Process C says: {0}", reader.ReadBoolean());
            }
            mutex.ReleaseMutex();
        }
    }
}

and the second process:

using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Threading;

class Program
{
    // Process B: 
    static void Main(string[] args)
    {
        try
        {
            using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap"))
            {

                Mutex mutex = Mutex.OpenExisting("testmapmutex");
                mutex.WaitOne();

                using (MemoryMappedViewStream stream = mmf.CreateViewStream(1, 0))
                {
                    BinaryWriter writer = new BinaryWriter(stream);
                    writer.Write(0);
                }
                mutex.ReleaseMutex();
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Memory-mapped file does not exist. Run Process A first.");
        }
    }
}
like image 37
Toan Nguyen Avatar answered Nov 03 '22 14:11

Toan Nguyen