0

I am trying to create a console application similar to speedsum site. The speedsum is a website which is really useful and fun to test our own mathematical ability in 30s.

After giving few try-s I was just thought to create one small C# console application with same concept.

Following is my code which is working fine. But I could not display countdown ?!

My code:

static void Main(string[] args)
    {
        int testCount = 0;

        Console.Write("\n Get.. Set... Go.... : This is a 30s test.. " +
            "Once each problem is completed the time finished will be shown \n Good Luck.. :) \n \n");
        Stopwatch watch = new Stopwatch();
        watch.Start();
        for (int i = 1; i < 100000; i++)
        {
            if (watch.Elapsed.TotalSeconds >= 30)
                break;
            TimeSpan timeSpan = TimeSpan.FromSeconds(Convert.ToInt32(watch.Elapsed.TotalSeconds));
            Console.Write($"\n {timeSpan.ToString("c")}" );

            Random r = new Random();
            int number1 = r.Next(10);
            int number2 = r.Next(10);
            int operation = r.Next(4);
            var method = (operation > 2) ? '+' : '*';
            int result = 0;
            result = method == '+' ? (number1 + number2) : (number1 * number2);             
            Console.Write($" \n {number1} {method} {number2} =  ");
            var getAnswer = Convert.ToInt32(Console.ReadLine());
            if (result == getAnswer)
            {
                testCount++;
                continue;
            }
            else
                break;
        }
        watch.Stop();

        if(testCount >= 1 && testCount <=5)
            Console.Write($"\n No Worries!! Try Hard ... \n you have solved {testCount} problems \n");
        else if(testCount >=6 && testCount <=10)
            Console.Write($"\n Good!! You can do well next time ... \n you have solved {testCount} problems \n");
        else
            Console.Write($"\n Awesome!! You are really a Genius ... \n you have solved {testCount} problems \n");

        Console.Write("\n Thank you for playing with me... \n Enter a key to exit");
        Console.Read();
    }

I would like to get the countdown from 30s to 0s at,

Get.. Set... Go.... : This is a 30s test.. Once each problem is completed the time finished will be shown 
Good Luck.. :)

<<Timer Should go here>> (30, 29... 0)

5 * 5 = 25 ... 

This SO Question Showing how to get countdown into our program, But I am confused at how I can do both parallely countdown and giving problems.

Any suggestion would be helpful to me.

Jeya Suriya Muthumari
  • 1,947
  • 3
  • 25
  • 47

1 Answers1

-2

It looks like you want to use BackgroundWorker. Then, DoWork event will decrement amount of seconds left, while ProgressChanged event will report current progress. The advantage of this solution is that background worker is running async, so you will not block your main thread allowing the user to enter answer anytime they want.

private static int secondsLeft;
private static BackgroundWorker bgWorker;

static void Main(string[] args)
{
    secondsLeft = 30;
    bgWorker = new BackgroundWorker();
    bgWorker.DoWork += bgWorker_DoWork;
    bgWorker.ProgressChanged += bgWorker_ProgressChanged;
    bgWorker.WorkerReportsProgress = true;
    bgWorker.RunWorkerAsync();

    Console.ReadLine();
}

private static void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
    while (secondsLeft >= 0)
    {
        bgWorker.ReportProgress(secondsLeft);
        Thread.Sleep(1000);
        secondsLeft--;
    }
}

private static void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    Console.WriteLine($"Seconds left: {e.ProgressPercentage}");
}
Dmitry Korolev
  • 675
  • 4
  • 20