---
title: "Can't stop a timer in c#"  
description: "Can't stop a timer in c#"  
author: "Anonymous User"  
published: 2013-12-18  
updated: 2013-12-18  
canonical: https://www.mindstick.com/forum/1804/can-t-stop-a-timer-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Can't stop a timer in c#

I want the [timer](https://www.mindstick.com/articles/52/creating-timer-at-runtime-in-c-sharp-dot-net) to stop when the ImageNumber is equal to zero.

```
 private void Health_Regen_Tick(object sender, EventArgs e)    {        if (ImageNumber1 == 0)        {           
       Health_Regen.Enabled = false;        }        if(ImageNumber1 < 20)        {           
ImageNumber1 += 1;           
HealthBar.Image = Image.FromFile(path + ImageNumber1.ToString() +".png");        }    }
```

If I [add](https://www.mindstick.com/forum/12983/add-address-in-textbox-when-page-is-load-and-if-i-search-address-in-textbox-show-in-map-by-javascript) a return statement after the first [if statement](https://www.mindstick.com/forum/12682/jquery-toggle-if-statement) the [second](https://answers.mindstick.com/qa/41761/how-did-the-second-industrial-revolution-influence-women-s-roles-in-society) if statement is disabled.

## Replies

### Reply by Anonymous User

Hi Jeet,\

Replace

if (ImageNumber1 == 0)

with

if (ImageNumber1 >= 20)

Your timer will already stop when ImageNumber1 equals 0, but it just never counts down to 0.

Also if you change ImageNumber1 to 0. It may already be running the timer which will increment it by one and skip the stopper, so pretty much the way you have it coded it's based on luck right now.

The luck happens if you change ImageNumber1 while timer is already running.

## Try this:

```
        if (ImageNumber1 == 0) {            Health_Regen.Enabled = false;        } else if (ImageNumber1 < 20) {            ImageNumber1 += 1;            HealthBar.Image = Image.FromFile(path + ImageNumber1.ToString() + ".png");        }
```

Still the luck may still happen best you can do is stop the timer outside the timer when you make it ImageNumber1 = 0;


---

Original Source: https://www.mindstick.com/forum/1804/can-t-stop-a-timer-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
