【问题标题】:Cant get a bool to work in this context在这种情况下无法使用布尔值
【发布时间】:2017-07-15 15:34:19
【问题描述】:

我确定这有一个非常简单的解决方案,但我对此很不擅长...... 我想要“时间到了!”仅在 2 分钟计时器后显示的文本,与开始时和 2 分钟后都适用,我认为 bool 会做到这一点,但我无法让它工作,我认为它与“静态空白”有关“公共无效”的东西,但我不确定,任何帮助将不胜感激!

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 语句中进行赋值。请解释您所说的“无法正常工作”是什么意思。
  • 为什么需要布尔值?当run 被执行时,a 似乎总是true
  • @LasseV.Karlsen:是的,它可以编译。请参阅我对赋值表达式的评论。
  • 我的错,我已经习惯于在自己的项目中将“所有警告都视为错误”,以至于我认为这不是错误,只是警告。

标签: c# timer boolean


【解决方案1】:

您当前代码中的问题如下:以下if statement 将始终为true

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

为什么会这样?if statement() 中,您应该传递一个有效的布尔表达式。当你写a = true时,你正在做的是

  1. true 分配给a
  2. if 的上下文中评估a(及其新值)。由于abool 类型,它确实可以编译。

因此if 总是得到满足,你总是得到打印。

您的意思可能是写a == true。区别在于== 是等于运算符而不是赋值运算符。


顺便说一下,如果你看一下 Visual Studio 会给你以下信息:

条件表达式中的赋值总是不变的;你的意思是用 == 代替 = 吗?

【讨论】:

  • @downvoter。在 C# 中,赋值是有效的表达式!它产生分配的值作为表达式的结果。请参阅:Simple assignment。它说:简单赋值表达式的结果是赋值给左操作数的值。
  • 好的,这确实解决了 VS 向我展示的问题,谢谢,但我在问题中忽略的是我得到了这个编译错误...错误 CS0120 需要对象引用对于非静态字段、方法或属性“Program.run(object)”,这是指 system.threading 行中的“run”,以及下面 a = true 行中的“a”...
【解决方案2】:

从@Real Caz 的How do you add a timer to a C# console application 线程中尝试答案

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();
    }
}
}

【讨论】:

  • 不使用相同的方法,但就像我说的,编程新手,这似乎工作得很好,谢谢:)
【解决方案3】:

您可以执行以下操作:

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);
    }
}

【讨论】:

    猜你喜欢
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 2020-04-07
    • 1970-01-01
    • 2015-06-16
    • 2019-12-14
    • 1970-01-01
    相关资源
    最近更新 更多