我有两个问题:
- 如何在C#中做延时,我想在程序启动时x秒后调用一个函数,但程序中的其他事情应该继续
- 是否只有当程序在 Debug模式下运行时才能调用函数或语句?
最佳答案
对于 #1,您需要一个 Timer
,它在程序启动时启动,并将其设置为在 x 秒后计时。您注册将被触发的计时器的 Tick
事件,然后您可以调用所需的方法。 重要提示:在 Tick 事件的处理程序中,在调用您的方法之前,您需要停止计时器。否则它将以 x 秒的间隔继续调用您的方法。
这是一个comparison between the different timers在 .NET 中可用。
对于 #2 你可以使用编译指令:
#if DEBUG
CallMyMethod();
#endif
您还可以使用 Conditional Attribute.
[Conditional("DEBUG")]
public void CallMyMethod()
{
Console.WriteLine("This method will be called only when in DEBUG mode.");
// notice the "DEBUG" string in the attribute parameter
}
关于c# - 如何延迟以及如何仅在 c# 的 Debug模式下启动函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4122595/