1

I'm working on writing automated UI tests and using singleton pattern (lazy) to store UI object references.

How can I dispose _appMenu.Value? Could anyone please help?

private static Lazy<AppMenu> _appMenu = new Lazy<AppMenu>(() => new AppMenu(_logger,_reporter));

public static AppMenu Instance
{
    get
    {
        return _appMenu.Value;
    }
}

private AppMenu(ILogger logger, IReporter reporter)
{
    _logger = logger;
    _reporter = reporter;
    AppMenuContainer = MainWindow.Instance.MainWindowContainer.FindSingle<TabPageList>(GenericProperties.AppMenu);
}
Suresh Raja
  • 411
  • 6
  • 23
  • See also https://stackoverflow.com/questions/8771961/how-can-i-ensure-that-i-dispose-of-an-object-in-my-singleton-before-the-applicat, https://stackoverflow.com/questions/8877552/how-and-when-to-dispose-garbage-collect-a-singleton-instance, https://stackoverflow.com/questions/6229079/disposing-a-singleton-instance-c, https://stackoverflow.com/questions/2931302/using-dispose-on-a-singleton-to-cleanup-resources, and https://stackoverflow.com/questions/36503588/self-closing-streamwriter-singleton, to name a few of the others you should have found when you searched for your answer here. – Peter Duniho Aug 09 '16 at 04:05
  • @PeterDuniho I looked through those. None of it uses Lazy implementation (except one of course) – Suresh Raja Aug 09 '16 at 04:10
  • First, "except one" is all you need. Second, that the implementation uses `Lazy` is really immaterial. All `Lazy` is, is a container. It's little more than an "enhanced variable". It doesn't change in any way the basic strategies available to you for disposing singletons, and as such every useful answer in the various duplicates are potentially useful in your case. – Peter Duniho Aug 09 '16 at 04:13
  • If after reviewing all of these and others, you are still stuck on a problem, please post a new question in which you've provided a good [mcve] that shows what you've tried, and explain what _specific_ issue you are having trouble with and which is not adequately addressed by the numerous duplicates. – Peter Duniho Aug 09 '16 at 04:13

0 Answers0