Skip to content

Add a flag AutoMeasurement.Initialized #35

Open

Description

@xmedeko

I am using AutoMeasurement class from a non-gui library and it need to know, if the AutoMeasurement is initialized or not. (AutoMeasurement.Instance throws an exception.) Some flag like

public static bool Initialized
{
    get { return _instance != null; }
}

Activity

  1. changed the title [-]Add Initialized flag to AutoMeasurement[/-] [+]Add a flag AutoMeasurement.Initialized[/+] on Sep 1, 2015
  2. JimiC commented on Oct 22, 2015

    @JimiC

    Before calling any method of AutoMeasurement, you _always_ have to declare an instance first. i.e.
    AutoMeasurement.Instance = new WinFormAutoMeasurement();

    @damieng This issue can be closed.

  3. xmedeko commented on Oct 22, 2015

    @xmedeko
    Author

    I have done a log4net Appender which sends errors to GA by CSharpAnalytics. I have to check if the AutoMeasurement is initialized or not. So far, I have implemented my own flag, but it would be nice to have such flag build in the library itself, so the code would not rely on any external flags.

  4. JimiC commented on Oct 22, 2015

    @JimiC

    Wouldn't code like AutoMeasurement.Instance != null suffice?

    Edit: Upon checking the above proposed code, I realized that it throws an exception.

  5. damieng commented on Oct 22, 2015

    @damieng
    Contributor

    The problem with this is it means now every single call to AutoMeasurement has to be wrapped in the null check. The intent was you could just call it and it would do it when it is finished initializing like a queue. (although obviously there are some race conditions right now)

  6. JimiC commented on Oct 22, 2015

    @JimiC

    The other way around is to encapsulate the null check of the Instance in a try/catch and use a self flag as @xmedeko might do.

  7. xmedeko commented on Oct 22, 2015

    @xmedeko
    Author

    @damieng Yep, this log4net appender has wrap every call to the initialized check. But it is a special pieace fo code. The rest of the application does not check for initialized before AutoMeasurement.Instance use. @JimiC try/catch is IMHO to costly comparing to the null check.

    Another solution instead of Initialized may be

    public static BaseAutoMeasurement InstanceOrNull { get { return _instance; } }

    Which is a hint to developer that Instance is a normal usage and InstanceOrNull is an exceptional usage. So they would not blindly wrap every call to Initialized check when it is not necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions