Avatar of Desired_Username
Desired_Username asked on

Repeating a task every few minutes

Im trying to process an event every x minutes after a button has been pressed in a windows form.  The current way im doing it uses a loop which shows the form as not responding after a few minutes.

Help as soon as possible please this has to be handed in soon

//Button press
        private void button2_Click(object sender, EventArgs e)
                button2.Enabled = false;
        private void DoProcess()
            for(int a = 0; ;a++)
                ////do the stuff here...... and min window
                this.WindowState = FormWindowState.Minimized;

Open in new window

C#Programming Languages-OtherWindows OS

Avatar of undefined
Last Comment

8/22/2022 - Mon


i'd either kick off DoProcess in a separate thread, or see whether DoEvents has any impact on your form response problem.

Why not use a timer to "do the stuff"?
If doing "the stuff" is processor intensive, then do it in a backgroundworker thread.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question

its not processor intensive no, my problem is that the process needs to be triggered when the button is hit.  But at the minute its entering DoProcess and getting stuck in my loop causing my form to crash.
Your help has saved me hundreds of hours of internet surfing.

You should have a flag to avoid that task can be invoked twice. You set it if it's clear, when you begin the process and clear again at finish.

You should not call Thread.Sleep for 40 seconds in your code.  If the thing you are processing is on the same thread then it will appear to be deadlocked.

Create a 5 minute timer (5 minutes = 300,000 milliseconds), but only start the timer when the button is clicked.  Call DoProcess in the timer event.