C# 控制台应用程序在将特定参数运行到主字符串时忽略 void 方法的 Writeline 输出?

发布于 2025-01-11 20:06:27 字数 1287 浏览 1 评论 0原文

我有这个代码。 在运行代码时如何忽略 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傻比既视感 2025-01-18 20:06:27

您可以在程序类中声明一个 Field 并通过 args 将其填充到 main 方法中,然后在计时器中检查它,如下所示:

namespace TimerExample
{
    class Program
    {
        static string[] Args;
        static Timer timer = new Timer(1000);
        static int i = 0;


        static void Main(string[] args)
        {     
         Args = 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);
            if(!Check(Args))
                 Console.Write("\r   Time Passed:  " + elapsedTime);
        }

        private static bool Check(string[] args) 
        { 
            foreach (var arg in args) { 
                if(arg == "YourArg") 
                    return true; 
                } 
                return false; 
        }
               
    }
}

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:

namespace TimerExample
{
    class Program
    {
        static string[] Args;
        static Timer timer = new Timer(1000);
        static int i = 0;


        static void Main(string[] args)
        {     
         Args = 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);
            if(!Check(Args))
                 Console.Write("\r   Time Passed:  " + elapsedTime);
        }

        private static bool Check(string[] args) 
        { 
            foreach (var arg in args) { 
                if(arg == "YourArg") 
                    return true; 
                } 
                return false; 
        }
               
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文