0
votes

I have 3 simple program and each is a simple window. I will start all 3 of the process and then click on program 1 or 2's button to show up the window of program 3.
Program 1 & 2: Only have 1 button. When clicked, shows the hidden process of program 3(which is also a window).
Program 3: Starts up as a hidden process, and it is waiting for program 1 and 2's message before popping up. Depending on the button press, the window caption should change to the caption of program 1 or 2.

I am not sure what function or do I use a thread to make this behavior? I believe I need to use some sort of thread to do this.. first make program 3 hidden and then waiting for the message of program 1 and 2.. any ideas?

EDIT: I am using C++ and I am told to use a semaphore.

2

2 Answers

1
votes

I would suggest using a Windows Event. Specifically, a Manual Reset Event. Your program 3 does a Wait on the event. When Program 1 or Program 2 wants to wake the window up, it sets the event. When Program 3 goes back to hiding, it clears the event.

You could use SendMessage or PostMessage, but the event seems much easier and straightforward. It also has certain advantages:

  • Program 1 and Program 2 don't need to find the window handle of Program 3, or broadcast a message that could be intercepted by some other process.
  • You can add security attributes to the event to prevent rogue programs from accessing it.
  • You can use the technique from a console application, a Windows service, or any other process, regardless of whether it's operating a message loop.
  • It's easier (for me, anyway) to understand than using Windows messages.

This isn't an appropriate use for a semaphore. A semaphore is typically used to synchronize access to multiple shared resources. All you want here is for Program 3 to wait for a notification, and for Program 1 or Program 2 to be able to send that notification.

If you have to pass data from Program 1 to Program 3, then the Event won't help you do that. You'll have to come up with a communication method such as a memory mapped file, pipe, network socket ... or even a Windows message in that case. But for simple "Hey, wake up!" notification, I'd use an event.

1
votes

Since you need to communicate a simple message across a process boundary, I'd recommend using something in the SendMessage family. You will first need to acquire a handle for the target window. This function is pretty low-level in the windowing API, and so you'll only be able to get at it directly from C/C++, but you didn't specify what language you were using, and I think there are wrappers around this routine for the CLR accessible through C# as well.