PC Review


Reply
Thread Tools Rate Thread

ContextMenu - reproducible crash, CF 1.0 SP2

 
 
Neville Lang
Guest
Posts: n/a
 
      23rd Sep 2004
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


 
Reply With Quote
 
 
 
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      23rd Sep 2004
Replied offline

--
Alex Feinman
---
Visit http://www.opennetcf.org
"Neville Lang" <neville@MAPS_ONnjlsoftware.com> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Neville Lang
Guest
Posts: n/a
 
      23rd Sep 2004
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" <(E-Mail Removed)>
To: "Neville Lang" <(E-Mail Removed)>
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





 
Reply With Quote
 
Neville Lang
Guest
Posts: n/a
 
      27th Sep 2004
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



"Neville Lang" <neville@MAPS_ONnjlsoftware.com> wrote in message
news:(E-Mail Removed)...
> 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" <(E-Mail Removed)>
> To: "Neville Lang" <(E-Mail Removed)>
> 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
>
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reproducible Explorer bug Nick Payne Windows Vista File Management 0 6th Nov 2007 10:08 AM
Is it possible to add ContextMenu items to the Standard TextBox ContextMenu??? =?Utf-8?B?TWlrZSBpbiBQYXJhZGlzZQ==?= Microsoft Dot NET Framework Forms 1 15th Mar 2004 07:54 AM
Reproducible bug. How to report? A Venier Microsoft Outlook 0 28th Jan 2004 08:28 PM
ASP.net reproducible bug Ryan Folstad Microsoft ASP .NET 5 12th Aug 2003 01:14 AM
reproducible combo box bug guy Microsoft Dot NET 0 17th Jul 2003 02:48 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:30 PM.