0

Greetings I'm facing some issues while trying to make a simple event scheduler. In the code bellow my intention is to Increase or Decrease Time (when a button is pressed using mouse events).

However after many attempts I can't figure out why I can Increase; but not Decrease time.

    // Timer
    private Timer TmrCount;

    private int HH {get;set;}
    private int MM { get; set; }

    private Button CurrBtn = new Button();

    #region <Mouse Events>
    private void OnMouseDown(object sender, EventArgs e)
    {
        CurrBtn = (Button)sender;

        StartTimer();
    }

    private void OnMouseUp(object sender, MouseEventArgs e)
    {
        StopTimer();
    }
    #endregion

    #region <Timer>
    private void StartTimer()
    {
        if (TmrCount == null)
        {
            TmrCount = new Timer();
            TmrCount.Interval = 210;
            TmrCount.Tick += TmrCount_Tick;
            TmrCount.Start();
        }
    }

    private void TmrCount_Tick(object sender, EventArgs e)
    {
        Set_Time();
    }

    private void StopTimer()
    {
        if (TmrCount != null)
        {
            TmrCount.Stop();
            TmrCount.Dispose();
            TmrCount.Tick -= TmrCount_Tick;
            TmrCount = null;
        }
    }

    private void Set_Time()
    {
        switch (CurrBtn.Text)
        {
            case "+":
                // Condition Check (Increase HH))
                //if (HH == 23) { HH = default(int); }

                //Increase HH
                //if (HH < 23) { HH += 1; }

                while (HH < 23) { HH++; break; }
                break;

            case "-":
                // Condition Check (Decrease HH)
                //if (HH == default(int)) { HH = 23; }
                //if (HH == 0) { HH = 23; }

                // Decrease HH
                while (HH > 23) { HH-=1; break; }
                break;
        }

        // Set Hour Text into Label
        lbl_HH.Text = Convert.ToString(HH);
    }
    #endregion

Can anyone point me the right direction? Thanks in advance

1 Answers1

0

Supposing that your HH variable start with a value of 1 then you should change your SetTime in this way

int HH = 1;

private void Set_Time()
{
    switch (CurrBtn.Text)
    {
        case "+":
            // Condition Check (Increase HH))
            // Within a limit of 23
            while (HH < 23) { HH++; break; }
            break;

        case "-":
            // Condition Check (Decrease HH)
            // Decrease HH but don't allow it to be less than 0 
            while (HH >= 0) { HH-=1; break; }
            break;
    }

    // Set Hour Text into Label
    lbl_HH.Text = Convert.ToString(HH);
}

By the way, after you dispose an object you should never try to access it even to remove an event handler.

private void StopTimer()
{
    if (TmrCount != null)
    {
        TmrCount.Stop();
        TmrCount.Tick -= TmrCount_Tick;
        TmrCount.Dispose();
        TmrCount = null;
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    You are reusing the variable _TmrCount_ and every time you call StartTime you assign a new reference (of a Timer object) to it. So what makes the difference here is your test for null. It is a good practice to call Dispose on disposable objects but in this simple context the GC is able to free that memory nevertheless. – Steve Apr 08 '18 at 18:31
  • 3
    @RicardoGarcia "Dispose means that you mark your object to be removed from memory by the GC" comment by Steve is let's say incomplete (I assume Steve tried to pack decent explanation in one sentence leading to that). There is no way to "mark object for GC", `Dispose` has nothing to do with GC and actually primarily needed due to entities not covered by GC at all (like unmanaged memory allocations)... You may want to read real explanation - https://stackoverflow.com/questions/2344240/what-is-relation-between-gc-finalize-and-dispose and search https://www.bing.com/search?q=c%23+dispose+gc – Alexei Levenkov Apr 08 '18 at 19:24
  • 1
    @AlexeiLevenkov this comment is straight up misleading and not a "try to pack a decent explanation", especially part about "you shouldn't risk a spectacular crash if the GC decides for whatever reason that they need that memory immediately. ". – Evk Apr 08 '18 at 20:22
  • Well the comments went a little bit further off topic (as they should; but it is against stackoverflow rules). Thus in order to let users follow a path for Dispose: https://stackoverflow.com/questions/2926869/do-you-need-to-dispose-of-objects-and-set-them-to-null –  Apr 08 '18 at 20:49