index out of range

Topics related to the Krypton Navigator.

Moderators: Phil Wright, Chris Porter

index out of range

Postby JoostS » Mon Jan 19, 2009 10:58 pm

I have a treeview with multiple items, and each item is related to a kryptonpage with a guid. I use a list with the guids and the kryptonpages to select or add a page.

Code example:
Code: Select all
public void SelectForm(Guid guid)
        {
            if (forms.ContainsKey(guid))
            {
                KryptonPage tabPage = forms[guid].TabPage;
                if (tabPage == null)
                {
                      FormBase form = CreateFormInstance(forms[guid]);
                      if (form != null)
                      {
                           tabPage = CreateTabItem(form);
                      }
                      else return;
                }

                if (tabPage != null)
                {
                    forms[guid].TabPage = tabPage;

                    if (!tabControl.Pages.Contains(tabPage))
                    {
                        tabControl.Pages.Add(tabPage);
                    }

                    tabControl.SelectedPage = tabPage;
                }
            }
        }


In the treeview mouseclick event I call this method. The problem is, when I click too fast I get the following exception on the tabControl.Pages.Add(tabPage);

De index moet binnen de grenzen van de lijst liggen.
Parameternaam: index, bij System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
bij System.Collections.Generic.List`1.Insert(Int32 index, T item)
bij ComponentFactory.Krypton.Toolkit.ViewComposite.Insert(Int32 index, ViewBase item)
bij dr.a(Object A_0, TypedCollectionEventArgs`1 A_1)
bij ComponentFactory.Krypton.Toolkit.TypedHandler`1.Invoke(Object sender, TypedCollectionEventArgs`1 e)
bij ComponentFactory.Krypton.Toolkit.TypedCollection`1.OnInserted(TypedCollectionEventArgs`1 e)
bij ComponentFactory.Krypton.Toolkit.TypedCollection`1.Add(T item)
bij Pits.Forms.Base.DockingBase.SelectForm(Guid guid) in D:\Project\Edumar_pits\PITS\Pits\Forms\Base\DockingBase.cs:regel 166

Is there a way to prevent this?

Regards,

Joost
JoostS
 
Posts: 35
Joined: Mon Jan 12, 2009 6:12 pm

Re: index out of range

Postby Phil Wright » Tue Jan 20, 2009 2:14 pm

Check for re-entrancy of the click event and prevent it calling the routine twice at once.
Phil Wright
Site Admin
 
Posts: 2720
Joined: Thu Apr 13, 2006 2:55 pm
Location: Melbourne, Australia

Re: index out of range

Postby JoostS » Tue Jan 20, 2009 11:12 pm

Can you please explain it a little bit more? Or some pseudo code if possible?
JoostS
 
Posts: 35
Joined: Mon Jan 12, 2009 6:12 pm

Re: index out of range

Postby Phil Wright » Wed Jan 21, 2009 12:23 pm

When you handler is called it might have some code inside that causes the handler to be called again. In that case you have re-entrancy, i.e. the handler has been re-entered when it was processing inside the handler. This is a very bad thing and usually leads to bugs and problems. I suggest you add the following to the start of the handler...

Console.WriteLine("Enter");

...and this to the end...

Console.WriteLine("Exit");

You your code until it crashes and check if the output shows it has two 'Enter' outputs in a row. If so then you have re-entrancy.
Phil Wright
Site Admin
 
Posts: 2720
Joined: Thu Apr 13, 2006 2:55 pm
Location: Melbourne, Australia

Re: index out of range

Postby JoostS » Wed Jan 21, 2009 6:36 pm

Okay thank you for the tip, I never heard of that term before. But I don't think that is the case. When I log it I get the following output:

Enter_SelectForm 4c12313c-086a-4e4b-a5ae-db53e4d062ae
Enter_SelectForm 1462829b-f951-4656-a699-5a122be62ff4
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Enter set tabControl.SelectedPage 4c12313c-086a-4e4b-a5ae-db53e4d062ae
Exit set tabControl.SelectedPage 4c12313c-086a-4e4b-a5ae-db53e4d062ae
Exit_SelectForm 4c12313c-086a-4e4b-a5ae-db53e4d062ae

I think I can see what the problem is, but don't know how to resolve it. The function SelectForm(Guid guid) is called when it is still busy selecting the previous kryptonPage (correct me if I am wrong) and that's where it goes wrong. I already tried to put a lock(tabControl) { } in the function but this has no effect.
JoostS
 
Posts: 35
Joined: Mon Jan 12, 2009 6:12 pm

Re: index out of range

Postby Phil Wright » Wed Jan 21, 2009 7:07 pm

Unfortunately there is little I can do to help as it is a design issue with your software. But you could try checking if the code is re-entering and ignore the second call then it occurs.
Phil Wright
Site Admin
 
Posts: 2720
Joined: Thu Apr 13, 2006 2:55 pm
Location: Melbourne, Australia

Re: index out of range

Postby Chris Porter » Thu Jan 22, 2009 1:02 am

One way you might be able to reduce the chance of re-entry is to manually handle your event handler subscriptions. As soon as the user clicks, remove the Click event handler, execute your code, and then re-subscribe the Click event handler.
Chris Porter
 
Posts: 714
Joined: Fri Oct 12, 2007 5:39 am
Location: Oklahoma City, OK, US


Return to Krypton Navigator

Who is online

Users browsing this forum: No registered users and 1 guest