Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a flag AutoMeasurement.Initialized #35

Open
xmedeko opened this issue Sep 1, 2015 · 6 comments
Open

Add a flag AutoMeasurement.Initialized #35

xmedeko opened this issue Sep 1, 2015 · 6 comments

Comments

@xmedeko
Copy link

xmedeko commented Sep 1, 2015

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; }
}
@xmedeko xmedeko changed the title Add Initialized flag to AutoMeasurement Add a flag AutoMeasurement.Initialized Sep 1, 2015
@JimiC
Copy link

JimiC commented Oct 22, 2015

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.

@xmedeko
Copy link
Author

xmedeko commented Oct 22, 2015

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.

@JimiC
Copy link

JimiC commented Oct 22, 2015

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

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

JimiC added a commit to JimiC/CSharpAnalytics that referenced this issue Oct 22, 2015
@damieng
Copy link
Contributor

damieng commented Oct 22, 2015

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)

@JimiC
Copy link

JimiC commented Oct 22, 2015

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.

@xmedeko
Copy link
Author

xmedeko commented Oct 22, 2015

@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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants