views:

45

answers:

1

I have an application which has to monitor 211 rods, and every 5 seconds it will update 2 ListBox controls, each one containing either the inserted rods or the removed ones. When I manually use the button for inserting/removing rods the code executes perfectly and the ListBoxes update properly. When I use the global button which inserts all 211 one of the ListBox controls stops working properly.

The code for ListBox update

bool IClear = true, RClear = true;
        for (int foo = 0; foo < Rods.Count; foo++)
        {
            if (Rods[foo].State == RodState.Inserted)
            {
                UpdateRodList update = new UpdateRodList(UpdateIRodUI);
                if (IClear)
                {
                    InsertedRods.Dispatcher.BeginInvoke(update, System.Windows.Threading.DispatcherPriority.Normal, foo, true);
                    IClear = false;
                }
                else
                {
                    InsertedRods.Dispatcher.BeginInvoke(update, System.Windows.Threading.DispatcherPriority.Normal, foo, false);
                }
            }
            if (Rods[foo].State == RodState.Removed)
            {
                UpdateRodList update = new UpdateRodList(UpdateRRodUI);
                if (RClear)
                {
                    RemovedRods.Dispatcher.BeginInvoke(update, System.Windows.Threading.DispatcherPriority.Normal, foo, true);
                    RClear = false;
                }
                else
                {
                    RemovedRods.Dispatcher.BeginInvoke(update, System.Windows.Threading.DispatcherPriority.Normal, foo, false);
                }
            }
        }

The code for the insert button (the remove one is similar)

Int32[] RodsID = null;
        bool bParsed = false;
        if (RemovingRods_.Text.Contains("*"))
        {
            RodsID = new Int32[211];
            for (int i = 0; i < 211; i++)
            {
                RodsID[i] = i;
            }
            RemovingRods_.Text = "";
            bParsed = true;
        }
        if (RemovingRods_.Text.Contains("-"))
        {
            string stext = RemovingRods_.Text;
            Int32 a = Int32.Parse(RemovingRods_.Text.Substring(0, RemovingRods_.Text.IndexOf("-")));
            Int32 b = Int32.Parse(RemovingRods_.Text.Substring(RemovingRods_.Text.IndexOf("-") + 1));
            RodsID = new Int32[b - a];
            for (int i = 0; i < b - a; i++)
            {
                RodsID[i] = i + a;
            }
            RemovingRods_.Text = "";
            bParsed = true;
        }
        if (!bParsed)
        {
            string[] RodsID_;
            char[] split = { ' ' };
            RodsID_ = RemovingRods_.Text.Split(split);
            RemovingRods_.Text = "";
            RodsID = new Int32[RodsID_.Length];
            for (int i = 0; i < RodsID_.Length; i++)
            {
                RodsID[i] = Int32.Parse(RodsID_[i]);
            }
        }
        foreach (int numb in RodsID)
        {
            if (Rods[numb].Type == "Control Rod")
            {
                ControlRod Rod = new ControlRod();
                Rod.Number = numb;
                Rod.RodState = RodState.Changing;
                RemovingCRods.Add(Rod);
            }
            if (Rods[numb].Type == "Shortened Control Rod")
            {
                ShortenedControlRod Rod = new ShortenedControlRod();
                Rod.Number = numb;
                Rod.RodState = RodState.Changing;
                RemovingSRods.Add(Rod);
            }
            if (Rods[numb].Type == "Automated Control Rod")
            {
                // Automated Rods -- NO MANUAL CONTROL
            }
        }

And the global button code

try
        {
            Int32[] RodsID = null;
            string text = "0-211";
            RodsID = new Int32[211];
            for (int i = 0; i < 211; i++)
            {
                RodsID[i] = i;
            }
            foreach (int numb in RodsID)
            {
                if (Rods[numb].Type == "Control Rod")
                {
                    ControlRod Rod = new ControlRod();
                    Rod.Number = numb;
                    Rod.RodState = RodState.Changing;
                    InsertingCRods.Add(Rod);
                }
                if (Rods[numb].Type == "Shortened Control Rod")
                {
                    ShortenedControlRod Rod = new ShortenedControlRod();
                    Rod.Number = numb;
                    Rod.RodState = RodState.Changing;
                    InsertingSRods.Add(Rod);
                }
                if (Rods[numb].Type == "Automated Control Rod")
                {
                    AutomatedControlRod Rod = new AutomatedControlRod();
                    Rod.Number = numb;
                    Rod.RodState = RodState.Changing;
                    InsertingARods.Add(Rod);
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

What happens is when I press the global one, the Removed Rods ListBox will have all the rods as it should, and the Inserted Rods ListBox will contain the rods that were inserted before I pressed the button. It's as if when I pressed this button, this control doesn't update. P.S. If I remove rods manually using the button or insert it works perfectly.

As for the code requested by Marko:

 private void UpdateIRodUI(Int32 foo, Boolean clear)
    {
        if (clear)
        {
            InsertedRods.Items.Clear();
        }
        InsertedRods.Items.Add(Rods[foo].Number + " : " + Rods[foo].Type + " (" + foo.ToString() + ")");
    }

    private void UpdateRRodUI(Int32 foo, Boolean clear)
    {
        if (clear)
        {
            RemovedRods.Items.Clear();
        }
        RemovedRods.Items.Add(Rods[foo].Number + " : " + Rods[foo].Type + " (" + foo.ToString() + ")");
    }

Update: I have put the update ListBox code in a seperate function and took Marko's advice and also put in a function the InsertRods. Everything works fine now, but it seems that after I press the "emergency" button the InsertedRods ListBox updates and works just fine but RemovedRods just stops updating, unless I do it manually (it's supposed to update every 5 seconds through a Tick event). I even tried inserting all the rods, updating the ListBoxes and the clearing the "faulty" ListBox and still nothing, same result.

+1  A: 

I just took a quick glance of your posted code without focusing very deeply on it and a couple of questions popped into mind:

1) You posted your code for ListBox update, but it's unclear from the other two code pieces that where do you call the ListBox update method?

2) The code that you posted for "insert button" looks more like the code from the "remove button", because of Removing_Rods.Add()... But why do you duplicate your insert/remove button code in your global button code? Why not have an insert method, that both the insert button and global (insert) button call? And the same for remove. If you need to slightly alter the code based on whether the caller is the insert button or the global button, you can pass in a variable and check it inside the insert method.

3) Have you tried debugging your code? As in whether the listbox update method is called when the global button code is executed...

Marko
2) Yes, my bad that code is for "Remove Rods". I am going to try writing it into a function, thanks for the tip. As for 3) I haven't but I'm assuming it partially works since one ListBox updates, while the other one doesn't.
Andrew
Someone please ?
Andrew
Because of 1), it is impossible to tell why your listbox doesn't update. The code you provided never seems to call listbox update, perhaps that is the reason why you are having your problem. Because of your duplicate code, the global button might be missing the listbox update call. As for 3), that is the first thing any programmer should do before even thinking about google or asking a question on stackoverflow. In the movie Undersiege 2, there was a perfect remark - "Assumption is the mother of all f*ckups!".
Marko
Ah, just noticed that you have updated your question a bit. We'll you have added a method UpdateRRodUI(), and I assume that RemovedRods is the actual listbox (my advice is to prefix all UI controls with their actual types, for instance listBoxRemovedRods), but I still don't see any calls being made to UpdateRRodUI(). Thus the best advice I can give you is to debug your code, set a break point at UpdateRRodUI() and see if that even gets hit. If not then set a break point in that bit of code that handles rods and step through it line by line to see why UpdateRRodUI is never called...
Marko