Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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:
 
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 9-Jan-11 20:45pm
Comments
Tarun.K.S at 10-Jan-11 1:57am
   
Well for 10 seconds the UI won't respond because it is in the Main Thread.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
v2
Comments
Espen Harlinn at 6-Feb-11 15:03pm
   
Good answer, a 5
SAKryukov at 6-Feb-11 18:52pm
   
Thank you.
It was a funny question, right?
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Hiren's answer is totally correct.
You can try this code that will solve your problem:
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();
}
  Permalink  
v2
Comments
SAKryukov at 10-Jan-11 2:04am
   
Should work -- my 5
programmer095 at 12-Jan-11 1:00am
   
Thank you so much. I accept answer.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v3
Comments
JF2015 at 10-Jan-11 2:02am
   
Good answer! 5+
programmer095 at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 23 Jan 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100