From my testing it would appear that option 1 outlined in the questions edit has done the trick ... for the most part.
As has been stated, and of which I already understood, C# is a managed language; making memory management difficult.
However, by adding a new SecureString()
object to one of the ContextMenuStrip
menu items' Tag
attributes, the menu object will persist in RAM.
There are, sadly, two primary caveats to this approach:
1. If the machines RAM gets hit HARD, the SecureString
object is either forcibly discarded or decoupled from the encompassing object and the page fault/swap is performed. I am unsure as to whether it re-couples together again post scenario, but it seems to do ok.
2. If the machine is put to sleep or hibernate, the whole app gets page swapped and the SecureString
is most definitely discarded. To aid in these situations, I have a SessionSwitchEventHandler
that detects if the machine has been unlocked and creates a new SecureString()
object in a menu items' Tag
attribute and re-associates the ContextMenuStrip
object to the NotifyIcon.ContextMenuStrip
Although it's not the best solution, it does work and is better then creating a timer to touch the menu [shudders] or, worse, playing with the Process.MinWorkerSet
[takes a shower at the thought]
Thanks again to those who took part in the question and helped think further to find a solution.