C# 控制台应用程序在将特定参数运行到主字符串时忽略 void 方法的 Writeline 输出?
我有这个代码。 在运行代码时如何忽略 void 方法的输出一段时间?
这继续打印每秒的时间。 1秒循环 但我想避免其打印特定的主字符串参数正在运行。 我想继续按原样运行计时器。 只是不想从“private static void timer_Elapsed(object sender, ElapsedEventArgs e)”打印 Console.WriteLine("1st") 的 writeline
using System;
using System.Timers;
using threadTimer = System.Threading;
namespace TimerExample
{
class Program
{
static Timer timer = new Timer(1000);
static int i = 0;
static void Main(string[] args)
{
timer.Elapsed+=timer_Elapsed;
timer.Start();
Console.WriteLine("1st");
threadTimer.Thread.Sleep (10000);
Console.Clear ();
Console.Clear ();
Console.WriteLine("2nd");
threadTimer.Thread.Sleep (3000);
}
private static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
float TimeToMissionsReady = i++;
TimeSpan ts = TimeSpan.FromSeconds(TimeToMissionsReady);
string elapsedTime = String.Format("{0:00 Minutes} and {1:00 Seconds}",
ts.Minutes, ts.Seconds);
Console.Write("\r Time Passed: " + elapsedTime);
}
}
}
i have this code.
how can i ignore output from void method for some time while running my code?
This continue print time for each seconds. With 1 sec loop
But i want to avoid its print for specific Main string arguments are running.
i want to keep running timer as it is.
Just dont want to print writeline for Console.WriteLine("1st") from "private static void timer_Elapsed(object sender, ElapsedEventArgs e)"
using System;
using System.Timers;
using threadTimer = System.Threading;
namespace TimerExample
{
class Program
{
static Timer timer = new Timer(1000);
static int i = 0;
static void Main(string[] args)
{
timer.Elapsed+=timer_Elapsed;
timer.Start();
Console.WriteLine("1st");
threadTimer.Thread.Sleep (10000);
Console.Clear ();
Console.Clear ();
Console.WriteLine("2nd");
threadTimer.Thread.Sleep (3000);
}
private static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
float TimeToMissionsReady = i++;
TimeSpan ts = TimeSpan.FromSeconds(TimeToMissionsReady);
string elapsedTime = String.Format("{0:00 Minutes} and {1:00 Seconds}",
ts.Minutes, ts.Seconds);
Console.Write("\r Time Passed: " + elapsedTime);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在程序类中声明一个 Field 并通过 args 将其填充到 main 方法中,然后在计时器中检查它,如下所示:
you can declare a Field in program class and fill it in main method by args and check it in your timer if like this: