Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I'm trying to put together a small project used for training. A user must enter an answer to the problem, given a limited time. When time is expired, the user is not credited for the answer, correct answer shown. Of course, when the user presses Next button, the correct answer should not be shown until timeout is expired. During this time the user nust enter the answer. I tried to put delay in the Next button.

Simplified code to show the problem:

C#
using System;
using System.Threading;
using System.Windows.Forms;
namespace DelayButton
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Form MyForm = this;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            Thread.Sleep(10000);
            this.textBox1.Text = "Some answer...";
            button1.Enabled = true;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Can use second button!");
            CheckupAnswer(listBox1.SelectedIndex);
        }
        void CheckupAnswer(int answerIndex) { }
    }
}


It works, but during this time the user cannot select the answer. Also, hourglass cursor is shown, but much later and not always. I don't understand why. I don't need Next button during this time, so I disable it. How come other controls do not respond?

Could anyone help me to solve delay problem?
Posted
Comments
Tarun.K.S 10-Jan-11 1:57am    
Well for 10 seconds the UI won't respond because it is in the Main Thread.

You already have a good answers which can give you good idea on how to solve your problem methodically.

I want to add references to my small articles in CodeProject Tips/Tricks section. Many questions I answered have something in common, so I can reference my articles in the answers.

You can find a robust solution for hourglass cursor here:
Hourglass Mouse Cursor Always Changes Back to its Original Image. How?[^], please take a look not just at my article but at two alternatives -- they are very good.

Discussion around another question about multi-threading can be useful: Executing high volume inserts with threads[^].

See also my Tips/Trick article on inter-thread Invocation and posted samples: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].

You can can created a separate thread and start if from the very beginning (more exactly, when the application main form is shown). The thread should wait for the user's answer submitted to the message queue. When the answer is supplied, the thread can sleep for a specified period of time and than signal to the main form about time expiration. This condition should be signaled to the UI thread using Control.Invoke.

Good luck!
—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 6-Feb-11 15:03pm    
Good answer, a 5
Sergey Alexandrovich Kryukov 6-Feb-11 18:52pm    
Thank you.
It was a funny question, right?
--SA
Hi,

Hiren's answer is totally correct.
You can try this code that will solve your problem:
C#
System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
void tmr_Tick(object sender, EventArgs e)
{
  tmr.Stop();
  this.textBox1.Text = "Some answer...";
  button1.Enabled = true;
}

private void button1_Click(object sender, EventArgs e)
{
  button1.Enabled = false;
  tmr.Interval = 10000;
  tmr.Tick += new EventHandler(tmr_Tick);
  tmr.Start();
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 10-Jan-11 2:04am    
Should work -- my 5
programmer095 12-Jan-11 1:00am    
Thank you so much. I accept answer.
If you will put delay inside the code then code will just halt there and it will be just expressed by Hour glass. it will not go for further compilation if it isn't crossing delay line of code.

for solving your problem I hope THIS[^] will surely help you.

or you can also use TIMER[^] to acomplish that.
 
Share this answer
 
v3
Comments
JF2015 10-Jan-11 2:02am    
Good answer! 5+
programmer095 12-Jan-11 1:00am    
Thank you so much. I accept answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900