tags:

views:

48

answers:

1

I have an application that does the following:

one component looks for changes in a database
For every change, a row is added to a table storing change alert information

Another component looks for alerts
For each alert, an alert form pops up

Previously, I had my code only show one alert at a time, and things worked just fine. Now, I'm changing my code to allow multiple alert forms to open at the same time.

while(true)
{
    DB = //pseudocode: database object
    Var ResultAlert = 
        from a in DB.Alerts
        where a.Status == 0;

    foreach(var RowAlert in ResultAlert)
    {
        AlertForm f = new AlertForm(RowAlert.Id);
        f.Show();
    }
    Thread.Sleep(1000);
}

So as you can see, this code will check for new alerts every second, which is all well and good, but it could potentially open repeat alert forms each second. What I want to know is how to detect which forms are open (with corresponding alertIds) so I can only open new alert windows.

+1  A: 

You need to keep track of the forms that you've opened for each alert. Assuming that your Alerts table has an integer primary key (e.g. AlertId), you can track them in a dictionary:

var openForms = new Dictionary<int, AlertForm>();
while(true)
{
    DB = //pseudocode: database object
    Var ResultAlert = 
        from a in DB.Alerts
        where a.Status = 0;

    foreach(var RowAlert in ResultAlert)
    {   
        var alertId = RowAlert.AlertId;
        if(!openForms.ContainsKey(alertId))
        {
            AlertForm f = new AlertForm();
            openForms.Add(alertId, f);
            f.Show();
         }
    }
    Thread.Sleep(1000);
}

If you don't need to do anything with the open forms, then you could just store the IDs in a HashSet<int>.

Jamie Ide
@Jamie, Good to see another response from you today.Very helpful!I implemented what you have here and it's working beautifully :D
Soo