ContextMenu - reproducible crash, CF 1.0 SP2

N

Neville Lang

To Microsoft CF team and list,

I have previously posted information on this topic but I now want to open
another thread here after I have worked on this problem for a few days now
and want to share some information.

I have CF 1.0 SP2 and have been trying to get a ContextMenu working without
success. I keep getting a System.ArgumentException error.

What I am trying to achieve is to have a ToolBarButton set for a PushButton
style raise a ContextMenu when tapped. This part works as expected. As
background, I chose to use a ToolBarButton with PushButton style and a
separate ContextMenu instead of a ToolBarButton with the DropDownMenu style
set because I have found that for some Pocket PCs running PPC 2002 this
latter combination causes the Pocket PC to freeze after a couple of taps on
the menu. Having a ToolBarButton set as a PushButton and using a separate
ContextMenu overcomes the freeze problem on those Pocket PCs.

However, I also have various conditions in my app where I want to display
different menu items in the ContextMenu. When such a condition is
encountered in my app, I call a method and simply run
ContextMenu.MenuItems.Clear() and then begin using the Add() method to add
the new set of menu items. Theoretically, this should work every time, but
it does not.

I have now proven beyond doubt that a second call to my method that first
"clears" the MenuItems collection then "adds" the new ones will crash the
system IF the ContextMenu has not yet been displayed.

After much testing, it seems that the ContextMenu needs to be displayed
first each time after you Clear() then Add() a new set of menu items to
avoid the error message.

Displaying the context menu at the wrong time so a user can dismiss it just
to rebuild the next set of menu items is pretty ugly. This is the only
workaround I can find.

Does anybody know of a way to programmatically dismiss the ContextMenu in
order to provide this workaround?

For anyone interested, I can send you my VS .NET 2003 solution files that
will demonstrate this problem.

Does anyone have any other method of building different menu items for a
ContextMenu?

Does anyone know if CF 1.0 SP3 Beta or even CF 2.0 Beta fixes this problem?

Regards,
Neville Lang
 
N

Neville Lang

Alex,

From some more testing I did, I determined the problem was due to the
MenuItems still maintaining the Parent property. When the Parent property is
null, everything works as expected. When the Parent property has a value
then the System.ArgumentException is raised when you try to assign these
MenuItems to a "cleared" ContextMenu.

What I did was to move the call to DoInitial() from the Form1 constructor to
the PrepRepMenu method, positioned immediately after the
ContextMenu.MenuItems.Clear(). The DoInitial method re initializes the
MenuItem using the "new" operator. This action effectively sets the Parent
property to null. Though I have not looked at your code suggestion yet, I
suspect this is what you found also.

Essentially, if you need to rebuild a ContextMenu with a different set of
MenuItems based on some conditions, you not only have to use the
ContextMenu.MenuItems.Clear() method but you also need to re-initialize the
MenuItems that have been used previously in order to reset the Parent
property assignment on those MenuItems to null.

I will wait to hear what you find out about whether MenuItems can be
re-used. Since a ContextMenu can only use a MenuItem object, then I would
think that the Clear() method on the ContextMenu should clear the Parent
property of the MenuItems that it is releasing. In fact, this is probably
what is happening somewhere in the Show() method of the ContextMenu since
things are OK once you display the menu.

In the meantime, I now have a workaround to my problem.

Regards,
Neville Lang



----- Original Message -----
From: "Alex Feinman" <[email protected]>
To: "Neville Lang" <[email protected]>
Sent: Thursday, September 23, 2004 4:41 PM
Subject: RE: ContextMenu - reproducible crash, CF 1.0 SP2


Neville,

I am not hundred percent sure it's a bug, since there is a possibility
that it's by design, so I'll need to follow up on that. In the meantime
attached is a workaround that you can use. The problem is caused by the
fact that your menuitems still have non-null parent menu reference. One
would expect that clearing the parent menu menuitem collection will
reset the parent reference, but this is not the case. As you can see
with a bit of reflection the problem can be at least suppressed ina
relatively clean way, but like I said, I'd like to follow up on this -
it is possible that menutimes are not supposed to be reused.

Alex Feinman
 
N

Neville Lang

Hi all,

Just to complete this thread, Alex has tested CF v2.0 and has indicated that
the ContextMenu.Clear() method now fixes this problem, presumably by
resetting to null the Parent property of each MenuItem object that was
listed in its collection before clearing the collection.

Regards,
Neville Lang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top