0

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)

3
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by Mitja Bonca
Featured 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? Read More

  • There will be an event, it will be a Timer_Tick event. Read More

  • 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: [CODE] public partial class Form1 : Form { int U; Timer timer1; public Form1() { InitializeComponent(); label1.Text … Read More

  • 1
    __avd 1,826   6 Years Ago

    Thread might be another option, [code] 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; } }); … Read More

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

1

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?

Comments
++++++++++
1

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

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

Edited by __avd: Edit : put 50 instead of 5000

Comments
+++++
1

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

Comments
Thanks! I've changed.
This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.