Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在控制台应用程序中显示Thread.Sleep()的剩余时间_C#_.net_Timer_Console Application - Fatal编程技术网

C# 在控制台应用程序中显示Thread.Sleep()的剩余时间

C# 在控制台应用程序中显示Thread.Sleep()的剩余时间,c#,.net,timer,console-application,C#,.net,Timer,Console Application,我有一个无限运行的.net控制台应用程序。我已经插入Thread.Sleep()5分钟来暂停我的应用程序。 此时,我想显示线程变为活动状态并再次启动应用程序的用户剩余时间(秒) class Program { static void Main(string[] args) { Program program = new Program(); program.startFeed(); } public void startFeed()

我有一个无限运行的.net控制台应用程序。我已经插入
Thread.Sleep()
5分钟来暂停我的应用程序。 此时,我想显示线程变为活动状态并再次启动应用程序的用户剩余时间(秒)

class Program
{
    static void Main(string[] args)
    {
        Program program = new Program();
        program.startFeed();
    }
    public void startFeed()
    {
        while (true)
        {
            try
            {
                //My Application which i want to run continously 
                //when thread enters in run mode
            }
            catch (Exception xObj)
            {
                Console.WriteLine(DateTime.Now.ToString() 
                    + " >> Incoming Message Processing Error. >> " 
                    + xObj.Message);
            }
            Console.WriteLine("Waiting For Data......");
            Thread.Sleep(300000);
        }
    }
}

您的计划的当前结构不支持您试图实现的目标

调用时,它将在指定的时间内挂起当前线程。默认情况下,您的程序仅作为单个线程运行,因此挂起该线程将停止所有代码的运行

您希望向用户显示更新,但也希望工作进程在两次运行之间等待5分钟。这意味着您需要为辅助进程创建一个单独的线程,并从程序开始的线程管理该线程

正如其他人所建议的那样,一种更简单的方法是使用。在您的情况下,您可以将代码更改为以下内容:

class Program
{
    static void Main(string[] args)
    {
        Program program = new Program();
        program.startFeed();
    }

    // This is run on the main thread
    public void startFeed()
    {
        // Start a Timer on a new thread to do work with the ProcessData method
        // Pass null to its 'state' argument, wait 0 milliseconds before
        // running it, and run it once every 300000 milliseconds
        using (new Timer(ProcessData, null, 0, 300000))
        {
            // The Timer will only exist while we are inside the 'using' block;
            // stay here with a loop
            while (true)
            {
                // Write our status message
                Console.WriteLine("Waiting for data at {0}...", DateTime.Now);
                // We don't want this loop running ALL the time; add a small
                // delay so it only updates once every second
                Thread.Sleep(1000); 
            }
        }
    }

    // This is run on the background thread
    private void ProcessData(object state)
    {
        try
        {
            //My Application which i want to run continously 
            //when thread enters in run mode
        }
        catch (Exception xObj)
        {
            Console.WriteLine(DateTime.Now.ToString()
                + " >> Incoming Message Processing Error. >> "
                + xObj.Message);
        }
    }
}
很好,现在有两个线程同时运行,调用其中一个线程不会影响另一个线程。注意,您不需要调用ProcessData,因为ProcessData会为您处理

最后,您希望准确地向用户显示ProcessData将在何时再次运行。您可以通过在程序类中添加一个字段来实现这一点,例如
private DateTime\u lastRun
并且在ProcessData的开头,您可以将其设置为
DateTime.Now
。然后,在startFeed中的循环中,您可以使用类似于
\u lastRun.AddMinutes(5).Subtract(DateTime.Now).seconds的方法计算距离下一次运行还有多少秒


这里还有很多可以说的。正如其他人所暗示的,您正在编写轮询代码,而不是事件驱动代码。轮询通常比事件驱动的等效轮询更慢、效率更低、更复杂。但是,这取决于数据源是否能够在代码有新数据要处理时通知代码;投票可能是你唯一的选择


关于线程之间的通信还有很多要说的。就其本身而言,多线程是一个非常棘手的问题,也是许多难以发现的bug的原因。但是,对于本例,在两个线程之间写入控制台和设置共享日期时间字段应该是安全的

为什么不使用
System.Threading.Timer
进行此操作?@bryancrossby:能否删除评论中的问题部分?:“使用
System.Threading.Timer进行此操作!”:-P你能举个例子吗?@SagarDumbre只要在网上搜索一下,你就会发现很多例子。你等5分钟是重要的,还是愿意在物品一有货就处理?