locked
window forms: timer press the button every 60 seconds RRS feed

  • Question

  • Task:

    You have to press the "self control" button every 60 seconds, otherwise the login interface reappears, so that the porter has to register again.

    Below is the program, but you can only press it once and you have to log in again. How do I program the gatekeeper to press the button every 60 seconds?

    int duration = 60; // Duration of time
    private void timer1_Tick(object sender, EventArgs e)
            
            {
               duration--;
               textBox1.Text = duration.ToString();
               if (duration == 0)
               {
                   timer1.Stop();
                   MessageBox.Show("Entrance area not occupied");
                   this.Hide();
                   FormLogin fm = new FormLogin();
                   fm.ShowDialog(this);
                }
            }
    
    private void btn_selfcontrol_Click(object sender, EventArgs e)
            //Stopp the Timer
            {
                timer1.Stop();
            }

    Sunday, March 22, 2020 4:21 PM

All replies

  • Hi i did it in VB.Net Code 

    I think it is useful to take the idea or you can convert my code to C# 

    Give it a try

    Function Pwait(ByVal Myseconds As Double)
    
            Dim endTime As Date = Date.Now.AddSeconds(Myseconds)
            Do : Loop Until Date.Now > endTime
    
            Return 0
    
        End Function
    
        Sub Every60Second()
            Dim i As Integer = 60
    
            For j As Integer = 1 To 1440
                
                    Pwait(60)
                    MsgBox("hi " + Str(j))
                
            Next j
        End Sub

    You did't need the timer , Then put Ever60Second() in a suitable event.... You must remove the message Box because you will need to hit OK button every 60 seconds

    Here is the code in C# format ..I converted it by this site (https://converter.telerik.com)

     public void Pwait(double Myseconds)
        {
            DateTime endTime = DateTime.Now.AddSeconds(Myseconds);
            do
            {
            }
            while (!DateTime.Now > endTime);
    
            return 0;
        }
    
        public void Every60Second()
        {
            int i = 60;
    
            for (int j = 1; j <= 1440; j++)
            {
                Pwait(60);
                Interaction.MsgBox("hi " + Conversion.Str(j));
            }

    hope it helps


    Regards From Egypt





    • Edited by Amr_Aly Sunday, March 22, 2020 6:43 PM
    Sunday, March 22, 2020 5:41 PM
  • Consider this approach too:

    private void MyForm_Load( object sender, EventArgs e )

    {

       timer1.Interval = 60_000;

       timer1.Start( );

    }

     

    private void btn_selfcontrol_Click( object sender, EventArgs e )

    {

       timer1.Stop( );

       timer1.Interval = 60_000;

       timer1.Start( );

    }

     

    private void timer1_Tick( object sender, EventArgs e )

    {

       timer1.Stop( );

     

       // show Login form

       . . .

     

       timer1.Interval = 60_000;

       timer1.Start( );

    }

     

    Sunday, March 22, 2020 6:50 PM
  • Thank you very much.
    I have inserted a text box so that I can see the time as time goes by. How does that program?
    Sunday, March 22, 2020 7:46 PM
  • Hi Patrick96,

    If you have a textbox, you can do it like this.

            private System.Windows.Forms.Timer timer1;
            private int counter = 60;
    
            private void button1_Click(object sender, EventArgs e)
            {
                timer1 = new System.Windows.Forms.Timer();
                timer1.Tick += new EventHandler(timer1_Tick);
                timer1.Interval = 1000; // 1 second
                timer1.Start();
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
                counter--;
                if (counter == 0) 
                {
                    Console.WriteLine("Do Something");
                    timer1.Stop();
    
                    timer1.Start();
                    counter = 60;
                }
                textBox1.Text = counter.ToString();
            }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 23, 2020 6:04 AM
  • But that's terrible coding!  You have an infinite loop there that will consume 100% of one of your CPU.

    The timer **IS** the right answer.  Really.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Monday, March 23, 2020 8:43 PM
  • But that's terrible coding!  You have an infinite loop there that will consume 100% of one of your CPU.

    The timer **IS** the right answer.  Really.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Thanks Sir for paying attention .........

    I tested my code,It works well on my site if you test my code you will find that it ends after a specific period that i determined it before, This value "1440" represents the 24 hours per day (Because he asked for every 1 minute, so every 60 seconds the action he wants will perform and after another 60 seconds the action will perform again) ,,, 

    He can put this code in the "FORM LOAD" in order to perform for example a "BUTTON CLICK" it will click the button every 1 minute and when he closes the form the loop will DISPOSE .... And will start when he opens the form again ..... 

    By the way He did't put any limitation ,He said EVERY 60 SECONDS...This is what he asked for 

    "How do I program the gatekeeper to press the button every 60 seconds?

    Finally I'm here for learning by ask questions and in the same time answer or reply according to my small experience,

    So please if my answer is wrong or lead to any problem ,Don't hesitate to guide me to the right way.

    By the way i use this code for a long time without any problem , and it is very useful

    Accept my regards . 


    Regards From Egypt

    Monday, March 23, 2020 10:26 PM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 2, 2020 8:55 AM