Click here to Skip to main content
15,886,002 members
Articles / Desktop Programming / MFC

PI, A Small But Helpful Popup Info Implemented in C#

Rate me:
Please Sign up or sign in to vote.
4.28/5 (19 votes)
27 May 20043 min read 92.5K   357   43   16
A component for popup infos with configurable lifetime and callback features.

Sample Image - PI.gif

Introduction

One of the many small utilities that are probably reinvented every day is a user notification in a small popup window.

Typical usages are email notifications or status messages for long running processes. After joining the club of re-inventers, I hope to simplify the tasks for some readers by writing this article and submitting my solution called PI (very imaginative short for PopupInfo).

Using the Code

I tried to make using PI easy. Just add the PopupMessageHandler Component to your application and call:

C#
AddMessage(String yourMessage)

to start a new popup info.

The Component comes with designer support, so customization should be fairly easy.

The popup window appears at the lower right corner of the screen just above the taskbar and will disappear after a configurable time span.

Some care had to be taken to handle multiple popup windows. Of course, they should not overlap and the number of windows displayed simultaneously must be limited. The very simple solution to this is using a queue to store messages that must wait for free space. Clicking a popup info usually signals that the user has seen it, so it can be closed. This can open a gap in our nice pile of windows that must be closed. Windows above a gap have to drop into the free space, waiting messages will be displayed on top of the stack.

Now, to a more complex aspect - getting messages back from the popup windows. For this, more information is required and a second version of the AddMessage method has the additional parameters to specify what should happen if the user clicks on a message.

C#
public void AddMessage(String yourMessage, 
      Control receiver, PIDelegate pd, object arg)
yourMessage the message you want to show
receiver the control that will be used to invoke the delegate
pd the delegate that will be invoked
arg an object that will be passed with the delegate

The test program shows an example for this call. The required delegate has the form:

C#
public delegate void PIDelegate(object data)

Two more variants of AddMessage exist, with an additional int parameter that defines the time to live of the popups. If not specified, the component default ttl will be used.

Points of Interest

One tricky thing in the implementation was thread synchronization. The popup messages do run in their own threads (taken from a ThreadPool) and need to invoke a method in the main thread of a control. Fortunately, the control class contains several methods to perform this task and in this case, BeginInvoke was used.

The main loop of the PopupMessageHandler runs in a background thread that waits for a signal, if no messages are present. The AddMessage methods lock the queue that is used to store popup infos, so they are thread safe.

History

  • 28th May 2004 - Minor code update

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.


Written By
Web Developer
Germany Germany
After studying chemistry and biochemistry i received my PhD at the Max-Planck Society in Biochemistry/Bioinformatics. For the last 3 Years i have been working in a German Bioinformatics Company.
My main interests are biological databases and Workflow-Tools.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Lony Ma19-Dec-10 21:33
Lony Ma19-Dec-10 21:33 
QuestionStrange location problem Pin
z00z015-Oct-08 10:48
z00z015-Oct-08 10:48 
General.NET 2.0 Upgrade Pin
hkeys26-Oct-06 9:54
hkeys26-Oct-06 9:54 
AnswerRe: .NET 2.0 Upgrade Pin
Jim Weiler1-Jul-07 5:17
Jim Weiler1-Jul-07 5:17 
GeneralAlternative to passing Control Pin
Richard Deeming1-Jun-04 6:56
mveRichard Deeming1-Jun-04 6:56 
GeneralNew Version fixing focus Problem Pin
fuad.abdallah28-May-04 11:50
fuad.abdallah28-May-04 11:50 
GeneralFocus Pin
JDBP27-May-04 14:48
JDBP27-May-04 14:48 
GeneralRe: Focus Pin
fuad.abdallah27-May-04 21:00
fuad.abdallah27-May-04 21:00 
GeneralRe: Focus Pin
JDBP28-May-04 4:58
JDBP28-May-04 4:58 
GeneralRe: Focus Pin
fuad.abdallah28-May-04 7:06
fuad.abdallah28-May-04 7:06 
GeneralRe: Focus Pin
fuad.abdallah28-May-04 7:01
fuad.abdallah28-May-04 7:01 
GeneralRe: Focus Pin
JDBP28-May-04 7:29
JDBP28-May-04 7:29 
GeneralRe: Focus Pin
JDBP28-May-04 8:29
JDBP28-May-04 8:29 
GeneralRe: Focus Pin
Jim Weiler1-Jul-07 5:20
Jim Weiler1-Jul-07 5:20 
GeneralThreads Pin
JDBP27-May-04 5:49
JDBP27-May-04 5:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.