Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy would an integer property sometimes return a 0?
    primarykey
    data
    text
    <p>[Edit: I realized that the parameter that is failing is actually a double and not an integer. None of the integer timers fail according to the logs. Most of the timers and parameters are integers, but not all. Doubles are not atomic and the lack of locking may be the issue after all.]</p> <p>I have an application that uses a class that contains properties for configurable values. Most of the properties being used in the app are derived. The values are set at start up and not changed while the main portion of the application is running.</p> <pre><code>private int _TimerInterval; public int TimerInterval { get { return _TimerInterval; } } private int _Factor1; public int Factor1 { set { _Factor1 = value; _TimerInterval = _Factor1 * _Factor2; } get { return _Factor1; } } private int _Factor2; public int Factor2 { set { _Factor2 = value; _TimerInterval = _Factor1 * _Factor2; } get { return _Factor2; } } </code></pre> <p>I find that very rarely the value returned is apparently zero because of an exception.</p> <pre><code>Exception Message: '0' is not a valid value for 'Interval'. 'Interval' must be greater than 0. Exception Target Site: set_Interval </code></pre> <p>The calling code looks lime this:</p> <pre><code>exitTimer.Interval = _config.TimerInterval; </code></pre> <p>The program is multi-threaded but the call to the individual property is only used in one thread. Other properties of the class are called in other threads. I do see the issue on other timers with similar properties.</p> <p>If I trap the exception and retry the assignment it works.</p> <p>Could there be something happening at my timer that would cause the execption other than the property returning zero?</p> <p><strong>Update #1 - More code was requested</strong></p> <p>Each field is defined as a <code>cfXXX</code> (Configuration Field) constant. This ensures we don't misspell the field names. A corresponding default value for each property is defined as <code>DefXXX</code>. The <code>PareseXXX</code> functions (<code>ParseInt</code> in this sample) accepts the string value from the configuration lookup and converts it to the corresponding value type or the provided default if it fails. Failure would be from a missing XML record (new configuration option) or one that was incorrectly edited.</p> <p>Code to load initial configuration data:</p> <pre><code>// Main Form public fMain() { InitializeComponent(); config = new ConfigData(); config.LoadConfig(); // Other initializations } //ConfigData Class // XML config field names private const string cfFactor1 = "Factor1"; private const string cfFactor1 = "Factor2"; private const string cfFactor3 = "Factor3"; private const string cfFactor4 = "Factor4"; //Default values private const int DefFactor1 = 1; private const int DefFactor2 = 50; private const int DefFactor3 = 1; private const int DefFactor4 = 25; public void LoadConfig() { Factor1 = ParseInt(ConfigurationManager.AppSettings[cfFactor1], DefFactor1); Factor2 = ParseInt(ConfigurationManager.AppSettings[cfFactor2], DefFactor2); Factor3 = ParseInt(ConfigurationManager.AppSettings[cfFactor3], DefFactor3); Factor4 = ParseInt(ConfigurationManager.AppSettings[cfFactor4], DefFactor4); } int ParseInt(string numberString, int aDefault = 0) { int result; if (!int.TryParse(numberString, out result)) { result = aDefault; } return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload