Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 792735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:02:20+00:00 2026-05-14T22:02:20+00:00

I am using .Net 3.5 for now. Right now I am using a using

  • 0

I am using .Net 3.5 for now.

Right now I am using a using trick to disable and enable events around certain sections of code. The user can change either days, hours, minutes or total minutes, and that should not cause an infinite cascade of events (e.g. minutes changing total, total changing minutes, etc.) While the code does what I want, there might be a better / more straight-forward way. Do you know of any?

For brawny points:

This control will be used by multiple teams – I do not want to make it embarrassing. I suspect that I do not need to reinvent the wheel when defining hours in a day, days in week, etc. Some other standard .Net library out there must have it. Any other remarks regarding code? This using (EventHacker.DisableEvents(this)) business – that must be a common pattern in .Net … changing the setting temporarily. What is the name of it? I’d like to be able to refer to it in a comment and also read up more on current implementations. In the general case not only a handle to the thing being changed needs to be remembered, but also the previous state (in this case previous state does not matter – events are turned on and off unconditionally). Then there is also a possibility of multi-threaded hacking. One could also utilize generics to make the code arguably cleaner. Figuring all this out can lead to a multi-page blog post. I’d be happy to hear some of the answers.

P.S. Does it seem like I suffer from obsessive compulsive disorder? Some people like to get things finished and move on; I like to keep them open … there is always a better way.

// Corresponding Designer class is omitted.
using System;
using System.Windows.Forms;

namespace XYZ // Real name masked
{
    interface IEventHackable
    {
        void EnableEvents();
        void DisableEvents();
    }

    public partial class PollingIntervalGroupBox : GroupBox, IEventHackable
    {
        private const int DAYS_IN_WEEK      = 7;
        private const int MINUTES_IN_HOUR   = 60;
        private const int HOURS_IN_DAY      = 24;
        private const int MINUTES_IN_DAY    = MINUTES_IN_HOUR * HOURS_IN_DAY;
        private const int MAX_TOTAL_DAYS    = 100;

        private static readonly decimal MIN_TOTAL_NUM_MINUTES = 1; // Anything faster than once per minute can bog down our servers.
        private static readonly decimal MAX_TOTAL_NUM_MINUTES = (MAX_TOTAL_DAYS * MINUTES_IN_DAY) - 1; // 99 days should be plenty.
        // The value above was chosen so to not cause an overflow exception.
        // Watch out for it - numericUpDownControls each have a MaximumValue setting.

        public PollingIntervalGroupBox()
        {
            InitializeComponent();

            InitializeComponentCustom();
        }

        private void InitializeComponentCustom()
        {
            this.m_upDownDays.Maximum           = MAX_TOTAL_DAYS    - 1;
            this.m_upDownHours.Maximum          = HOURS_IN_DAY      - 1;
            this.m_upDownMinutes.Maximum        = MINUTES_IN_HOUR   - 1;
            this.m_upDownTotalMinutes.Maximum   = MAX_TOTAL_NUM_MINUTES;
            this.m_upDownTotalMinutes.Minimum   = MIN_TOTAL_NUM_MINUTES;
        }

        private void m_upDownTotalMinutes_ValueChanged(object sender, EventArgs e)
        {
            setTotalMinutes(this.m_upDownTotalMinutes.Value);
        }

        private void m_upDownDays_ValueChanged(object sender, EventArgs e)
        {
            updateTotalMinutes();
        }

        private void m_upDownHours_ValueChanged(object sender, EventArgs e)
        {
            updateTotalMinutes();
        }

        private void m_upDownMinutes_ValueChanged(object sender, EventArgs e)
        {
            updateTotalMinutes();
        }

        private void updateTotalMinutes()
        {
            this.setTotalMinutes(
                MINUTES_IN_DAY * m_upDownDays.Value + 
                MINUTES_IN_HOUR * m_upDownHours.Value + 
                m_upDownMinutes.Value);
        }

        public decimal TotalMinutes
        {
            get
            {
                return m_upDownTotalMinutes.Value;
            }
            set
            {
                m_upDownTotalMinutes.Value = value;
            }
        }

        public decimal TotalHours
        {
            set
            {
                setTotalMinutes(value * MINUTES_IN_HOUR);
            }
        }

        public decimal TotalDays
        {
            set
            {
                setTotalMinutes(value * MINUTES_IN_DAY);
            }
        }

        public decimal TotalWeeks
        {
            set
            {
                setTotalMinutes(value * DAYS_IN_WEEK * MINUTES_IN_DAY);
            }
        }

        private void setTotalMinutes(decimal nTotalMinutes)
        {
            if (nTotalMinutes < MIN_TOTAL_NUM_MINUTES)
            {
                setTotalMinutes(MIN_TOTAL_NUM_MINUTES);
                return; // Must be carefull with recursion.
            }
            if (nTotalMinutes > MAX_TOTAL_NUM_MINUTES)
            {
                setTotalMinutes(MAX_TOTAL_NUM_MINUTES);
                return; // Must be carefull with recursion.
            }
            using (EventHacker.DisableEvents(this))
            {
                // First set the total minutes
                this.m_upDownTotalMinutes.Value = nTotalMinutes;

                // Then set the rest
                this.m_upDownDays.Value = (int)(nTotalMinutes / MINUTES_IN_DAY);
                nTotalMinutes = nTotalMinutes % MINUTES_IN_DAY; // variable reuse.
                this.m_upDownHours.Value = (int)(nTotalMinutes / MINUTES_IN_HOUR);
                nTotalMinutes = nTotalMinutes % MINUTES_IN_HOUR;
                this.m_upDownMinutes.Value = nTotalMinutes;
            }
        }

        // Event magic
        public void EnableEvents()
        {
            this.m_upDownTotalMinutes.ValueChanged += this.m_upDownTotalMinutes_ValueChanged;
            this.m_upDownDays.ValueChanged += this.m_upDownDays_ValueChanged;
            this.m_upDownHours.ValueChanged += this.m_upDownHours_ValueChanged;
            this.m_upDownMinutes.ValueChanged += this.m_upDownMinutes_ValueChanged;
        }

        public void DisableEvents()
        {
            this.m_upDownTotalMinutes.ValueChanged -= this.m_upDownTotalMinutes_ValueChanged;
            this.m_upDownDays.ValueChanged -= this.m_upDownDays_ValueChanged;
            this.m_upDownHours.ValueChanged -= this.m_upDownHours_ValueChanged;
            this.m_upDownMinutes.ValueChanged -= this.m_upDownMinutes_ValueChanged;
        }

        // We give as little info as possible to the 'hacker'.
        private sealed class EventHacker : IDisposable
        {
            IEventHackable _hackableHandle;

            public static IDisposable DisableEvents(IEventHackable hackableHandle)
            {
                return new EventHacker(hackableHandle);
            }

            public EventHacker(IEventHackable hackableHandle)
            {
                this._hackableHandle = hackableHandle;
                this._hackableHandle.DisableEvents();
            }

            public void Dispose()
            {
                this._hackableHandle.EnableEvents();
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:02:20+00:00Added an answer on May 14, 2026 at 10:02 pm

    I would use a boolean field to stop the setTotalMinutes method from being executed several times and move the creation of the eventhandlers to the InitializeComponentCustom method.

    Something like this:

    public partial class PollingIntervalGroupBox : GroupBox
    {
        private const int DAYS_IN_WEEK = 7;
        private const int MINUTES_IN_HOUR = 60;
        private const int HOURS_IN_DAY = 24;
        private const int MINUTES_IN_DAY = MINUTES_IN_HOUR * HOURS_IN_DAY;
        private const int MAX_TOTAL_DAYS = 100;
    
        private static readonly decimal MIN_TOTAL_NUM_MINUTES = 1; // Anything faster than once per minute can bog down our servers.
        private static readonly decimal MAX_TOTAL_NUM_MINUTES = (MAX_TOTAL_DAYS * MINUTES_IN_DAY) - 1; // 99 days should be plenty.
        // The value above was chosen so to not cause an overflow exception.
        // Watch out for it - numericUpDownControls each have a MaximumValue setting.
        private bool _totalMinutesChanging;
    
        public PollingIntervalGroupBox()
        {
            InitializeComponent();
            InitializeComponentCustom();
        }
    
        private void InitializeComponentCustom()
        {
            this.m_upDownDays.Maximum = MAX_TOTAL_DAYS - 1;
            this.m_upDownHours.Maximum = HOURS_IN_DAY - 1;
            this.m_upDownMinutes.Maximum = MINUTES_IN_HOUR - 1;
            this.m_upDownTotalMinutes.Maximum = MAX_TOTAL_NUM_MINUTES;
            this.m_upDownTotalMinutes.Minimum = MIN_TOTAL_NUM_MINUTES;
    
            this.m_upDownTotalMinutes.ValueChanged += this.m_upDownTotalMinutes_ValueChanged;
            this.m_upDownDays.ValueChanged += this.m_upDownDays_ValueChanged;
            this.m_upDownHours.ValueChanged += this.m_upDownHours_ValueChanged;
            this.m_upDownMinutes.ValueChanged += this.m_upDownMinutes_ValueChanged;
        }
    
        private void m_upDownTotalMinutes_ValueChanged(object sender, EventArgs e)
        {
            setTotalMinutes(this.m_upDownTotalMinutes.Value);
        }
    
        private void m_upDownDays_ValueChanged(object sender, EventArgs e)
        {
            updateTotalMinutes();
        }
    
        private void m_upDownHours_ValueChanged(object sender, EventArgs e)
        {
            updateTotalMinutes();
        }
    
        private void m_upDownMinutes_ValueChanged(object sender, EventArgs e)
        {
            updateTotalMinutes();
        }
    
        private void updateTotalMinutes()
        {
            this.setTotalMinutes(
                MINUTES_IN_DAY * m_upDownDays.Value +
                MINUTES_IN_HOUR * m_upDownHours.Value +
                m_upDownMinutes.Value);
        }
    
        public decimal TotalMinutes { get { return m_upDownTotalMinutes.Value; } set { m_upDownTotalMinutes.Value = value; } }
    
        public decimal TotalHours { set { setTotalMinutes(value * MINUTES_IN_HOUR); } }
    
        public decimal TotalDays { set { setTotalMinutes(value * MINUTES_IN_DAY); } }
    
        public decimal TotalWeeks { set { setTotalMinutes(value * DAYS_IN_WEEK * MINUTES_IN_DAY); } }
    
        private void setTotalMinutes(decimal totalMinutes)
        {
            if (_totalMinutesChanging) return;
            try
            {
                _totalMinutesChanging = true;
                decimal nTotalMinutes = totalMinutes;
                if (totalMinutes < MIN_TOTAL_NUM_MINUTES)
                {
                    nTotalMinutes = MIN_TOTAL_NUM_MINUTES;
                }
                if (totalMinutes > MAX_TOTAL_NUM_MINUTES)
                {
                    nTotalMinutes = MAX_TOTAL_NUM_MINUTES;
                }
                // First set the total minutes
                this.m_upDownTotalMinutes.Value = nTotalMinutes;
    
                // Then set the rest
                this.m_upDownDays.Value = (int)(nTotalMinutes / MINUTES_IN_DAY);
                nTotalMinutes = nTotalMinutes % MINUTES_IN_DAY; // variable reuse.
                this.m_upDownHours.Value = (int)(nTotalMinutes / MINUTES_IN_HOUR);
                nTotalMinutes = nTotalMinutes % MINUTES_IN_HOUR;
                this.m_upDownMinutes.Value = nTotalMinutes;
            }
            finally
            {
                _totalMinutesChanging = false;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In your first example, main2.cpp defines a global variable i,… May 16, 2026 at 6:32 pm
  • Editorial Team
    Editorial Team added an answer Found it. A rogue drupal_goto() statement implemented by previous developer. May 16, 2026 at 6:32 pm
  • Editorial Team
    Editorial Team added an answer Well, I would have to work at that a bit… May 16, 2026 at 6:32 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm at the very beginning of learning ASP.NET WebForms. Right now I'm starting to
Right now I am using the following CSS: option { border-width: 1px; border-style: solid;
I am using VB .Net for this, so I don't have access to var
I am using the Asp.Net Caching mechanism for a highly frequent changing web app.
I've got a .net 3.5 website that calls thousands of different stored procs using
Using Vb.net/SQL Server 2000 updating a row via a gridview/sqldatasource Stored proc: @ISTag varchar(10),
On my site right now, I'm passing in data through a query string to
UPDATE: Focus your answers on hardware solutions please. What hardware/tools/add-in are you using to
ScottGu says One nice feature of IIS7 on Windows Vista is that you can
I have a long-running report and want to show a wait-spinner to the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.