Hi guys)
I have a class .... something like that -

public class CITY
    {
        public int U; 
 
    }

Please - tell me how to make the variables of the first five minutes U=1, and the second five minutes U=0, the third five minutes again U =1 etc.

Thanks in advance)

Recommended Answers

All 8 Replies

Hi mate. You can use a timer. And set it to change the int value on every 5 minutes. If now is 1 set it to 0 and vice versa.
You need some help?

commented: ++++++++++ +1

just..Mitja..can I do this without event ? or It'll be more simple with last one?

There will be an event, it will be a Timer_Tick event.

This is not a semple code you would want it. I have put a label on the form, just to let you know whats going on with the variable "U".
Check it out:

public partial class Form1 : Form
    {
        int U;
        Timer timer1;
        public Form1()
        {
            InitializeComponent();
            label1.Text = "";
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            U = 0;
            if (timer1 == null)
            {
                timer1 = new Timer();
                timer1.Interval = 5000; //5 seconds now!
                timer1.Tick+= new EventHandler(timer1_Tick);
                timer1.Start();
                label1.Text = U.ToString();
            }                           
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (U == 0)
                U = 1;
            else
                U = 0;
            label1.Text = U.ToString();
            timer1.Enabled = true; //continue running
        }
    }

Thank you very much, Mitja!

Piece of cake :)
bye, bye and good night
Mitja

Thread might be another option,

public class Test : System.ComponentModel.INotifyPropertyChanged
    {
        private int _no;
        private bool _stopped;

        public Test()
        {
            System.Threading.Thread th = new System.Threading.Thread(() =>
            {
                _stopped = false;

                while (!_stopped)
                {
                    System.Threading.Thread.Sleep(5000); //five seconds
                    if (No == 0)
                        No = 1;
                    else
                        No = 0;
                }

            });
            th.Start();
        }

        ~Test()
        {
            _stopped = true;
        }

        public int No
        {
            get { return _no; }
            set
            {
                _no = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("No"));

                }

            }
        }
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    }

This code will create an instance of "Test" and also display value of No property whenever it gets modified.

private void Form1_Load(object sender, EventArgs e)
        {
            Test a = new Test();
            a.PropertyChanged += (se, ea) =>
                {
                    Action action = () =>
                    {
                        label1.Text = a.No.ToString();
                    };
                    if(label1.InvokeRequired)
                    label1.Invoke(action);
                };
           
        }
commented: +++++ +1

Using Threads isa good option as well.
Only one correction:
System.Threading.Thread.Sleep(5000); //five seconds // not 50
:)

commented: Thanks! I've changed. +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.