0

Hey;

In my application I want to count number of seconds while the application is waiting for the user input.

I think I should use another thread but I never worked with multithreading. Is there an easier way ?

4
Contributors
5
Replies
7
Views
6 Years
Discussion Span
Last Post by rohand
Featured Replies
  • 1

    Start a [URL="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx"]Stopwatch[/URL] when you want to start timing, and stop it when you are done. Read More

  • This is might help you out: [CODE] using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Diagnostics; //dont forget to add this namespace!!! namespace Feb02Exercise { public partial class Form1 : Form { Timer timer1; Stopwatch stopWatch1; bool bTimerStopped; public Form1() { … Read More

0

what you really want ? you can use Tick event of Timer class and check conditions inside the event..

Hey;

In my application I want to count number of seconds while the application is waiting for the user input.

I think I should use another thread but I never worked with multithreading. Is there an easier way ?

0

Ok sorry for not being clear. Actually I want to be able to wait a character input from user and when it exceeds 10 seconds bypass Console.Read() and print an error message.

//I start a timer here
//When it exceeds 10 seconds stop Console.Read() function and move on
Console.Read();

if(timer > 10)
   Console.Write("You couldnt answer in 10 seconds.");
1

This is might help you out:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;  //dont forget to add this namespace!!!

namespace Feb02Exercise
{
    public partial class Form1 : Form
    {
        Timer timer1;
        Stopwatch stopWatch1;
        bool bTimerStopped;
        public Form1()
        {
            InitializeComponent();
            CreatingTimer();
        }

        private void CreatingTimer()
        {
            timer1 = new Timer();
            timer1.Tick+= new EventHandler(timer1_Tick);
            timer1.Interval = 100;

            stopWatch1 = new Stopwatch();
            stopWatch1.Start();
            timer1.Start();
        }

        private void timer1_Tick(object obj, EventArgs e)
        {
            if (stopWatch1.IsRunning)
                ShowingTime();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (stopWatch1.IsRunning)
            {
                if (textBox1.Text != String.Empty)
                {
                    stopWatch1.Stop();
                    bTimerStopped = true;
                    ShowingTime();
                }
                //else, it happens nothing, user must enter some value into textBox
            }
        }

        private void ShowingTime()
        {
            // Get the elapsed time as a TimeSpan value.
            TimeSpan ts = stopWatch1.Elapsed;
            // Format and display the TimeSpan value.
            if (!bTimerStopped)
                labelTime.Text = String.Format("Time elapsed: {0:00}:{1:00} sec", ts.Seconds, ts.Milliseconds);
            else
            {
                labelTime.Text = "You needed " + String.Format("{0:00}:{1:00} sec", ts.Seconds, ts.Milliseconds) + " to press the button.";
                bTimerStopped = false;
            }
        }
    }
}
0

Okay..you can use Timer class. First set the Interval = 1000 (milisecond). Now see the below code

//define one variable
private long lCntTick = 1000;

private void Timer1_Tick(object sender, EventArgs e)
{
    if (lCntTick == 1800000)           
    {
        Console.Write("You couldnt answer in 10 seconds.");
        break;  
    }
    lCntTick += 1000;                    
}

Ok sorry for not being clear. Actually I want to be able to wait a character input from user and when it exceeds 10 seconds bypass Console.Read() and print an error message.

//I start a timer here
//When it exceeds 10 seconds stop Console.Read() function and move on
Console.Read();

if(timer > 10)
   Console.Write("You couldnt answer in 10 seconds.");
This question has already been answered. 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.