-1

Im sure this has a very simple solution, but I am very bad at this... I want the "time up!" text to display only after the 2 min timer, as appose to both when it begins and after the 2 mins, I thought a bool would do it, but i cant get it to work, i think its to do with the "static void" "public void" stuff but im not sure, any help would be massively appreciated!

class Program
{
    bool a = false;

    static void Main(string[] args)
    {
        //Define thread
        Console.WriteLine("Start, you have 2 mins!");

        //120000 is milliseconds, so every 120 seconds it will run the thread function
        System.Threading.Timer threadingTimer = new Timer(run, 0, 0, 120000);
        a = true;
        Console.ReadLine();
    }

    //define thread function
    public void run(object args)
    {
        if (a = true)
        {
            Console.WriteLine("Times Up!");
        }

    }
}

if there is a simpler way of achiving this then that is also appreciated, thanks for taking a look!

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188

3 Answers3

1

The problem in your current code is as follows: The following if statement will always be true.

if (a = true)
{
    Console.WriteLine("Times Up!");
}

Why is that? In the () of an if statement you should pass a valid boolean expression. When you write a = true what you are doing is

  1. Assigning true to a
  2. Evaluating a (with its new value) it in the context of the if. As a is of type bool it does compile.

Therefore the if is always met and you always get the print.

What you probably meant is writing a == true. The difference is that == is the equals operator instead of the assignment operator.


By the way if you look visual studio gives you the following message:

Assignment in conditional expression is always constant; did you mean to use == instead of = ?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • @downvoter. In C# an assignment is a valid expression! It yields the assigned value as result of the expression. See: [Simple assignment](https://msdn.microsoft.com/en-us/library/aa691315(v=vs.71).aspx). It says: *The result of a simple assignment expression is the value assigned to the left operand.* – Olivier Jacot-Descombes Jul 15 '17 at 15:47
  • ok, that did solve an issue that VS was showing me, thanks for that but what i neglected to mention in my question is that im getting this compile error... Error CS0120 An object reference is required for the non-static field, method, or property 'Program.run(object)' this is in reference to the "run" in the system.threading line, and the "a" in the a = true line below... – rjs_chipmunk Jul 15 '17 at 16:20
-1

try the answer from this thread How do you add a timer to a C# console application by @Real Caz

using System;
using System.Timers;

namespace timer
{
class Program
{
    static Timer timer = new Timer(1000); 
    static int i = 10; //this is for 10 seconds only change it as you will

    static void Main(string[] args)
    {            
        timer.Elapsed+=timer_Elapsed;
        timer.Start();
        Console.Read();
    }

    private static void timer_Elapsed(object sender, ElapsedEventArgs e)
    {
        i--;
        Console.WriteLine("Time Remaining: " + i.ToString());
        if (i == 0) 
        {
            Console.WriteLine("Times up!");
            timer.Close();
            timer.Dispose();
        }

        GC.Collect();
    }
}
}
-1

You could do something like the following:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Start, you have 10 seconds!");
        ShowMessageAfterDelay(TimeSpan.FromSeconds(10), "Time has lappsed!").Wait();
        Console.ReadLine();
    }

    static async Task ShowMessageAfterDelay(TimeSpan timeSpan, string message)
    {
        await Task.Delay(timeSpan);
        Console.WriteLine(message);
    }
}