Skip to main content
 首页 » 编程设计

c#之有没有更好的方法来避免使用 winforms 的无限循环

2024年02月24日36duanxz

我现在使用的是 .Net 3.5。

现在我正在使用 using 技巧来禁用和启用某些代码部分周围的事件。用户可以更改天数、小时数、分钟数或总分钟数,这不应导致无限级联的事件(例如,分钟数改变总数、总分钟数改变等)虽然代码可以满足我的要求,但可能会有更好的/更直接的方式。你知道吗?

对于壮汉点:

这个控件将被多个团队使用——我不想让它变得尴尬。我怀疑在定义一天中的小时数、一周中的几天等时,我不需要重新发明轮子。其他一些标准的 .Net 库必须有它。关于代码的任何其他评论?此 using (EventHacker.DisableEvents(this)) 业务 - 这一定是 .Net 中的常见模式...临时更改设置。它的名字是什么?我希望能够在评论中引用它,并阅读更多关于当前实现的信息。在一般情况下,不仅需要记住正在更改的事物的句柄,还需要记住之前的状态(在这种情况下,之前的状态无关紧要 - 事件无条件地打开和关闭)。然后还有多线程hacking的可能。还可以利用泛型使代码更简洁。弄清楚所有这些可能会导致一篇多页的博客文章。我很乐意听到一些答案。

附言我是不是好像得了强制症?有些人喜欢把事情做完然后继续前进;我喜欢让它们保持打开状态……总有更好的方法。

// 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(); 
            } 
        } 
    } 
} 

请您参考如下方法:

我会使用一个 bool 字段来停止多次执行 setTotalMinutes 方法,并将事件处理程序的创建移至 InitializeComponentCustom 方法。

像这样:

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; 
        } 
    } 
}