2012年视觉工作室的日期时间 [英] Date time in visual studio 2012

查看:69
本文介绍了2012年视觉工作室的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我想在我的项目中使用while循环特定时间(例如15秒)。

像这样:



而(t <15){

.....

.....

.....


}



我的尝试:



// DateTime timeToStartUpAgain = new DateTime();

//while(DateTime.Now< timeToStartUpAgain){

DateTime date1 = new DateTime(2009,8,1,0,0,0);

hello i want to use in my project a while loop for specific time (15 seconds for example).
like this:

while(t<15){
.....
.....
.....

}

What I have tried:

//DateTime timeToStartUpAgain = new DateTime();
//while(DateTime.Now < timeToStartUpAgain) {
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);

推荐答案

我无法想象为什么它有用;这对我来说听起来像是一种虐待。



无论如何,最好的办法是使用 System.Diagnostics.Stopwatch 。你在循环之前启动它并检查循环并在它休息时打破它:秒表类(System.Diagnostics) [ ^ ]。



当然,准确度不能比单次迭代的时间。您还可以在迭代代码的不同部分中多次检查时间。最终,如果代码是高度结构化的,您可以在不同的位置和不同的级别进行检查,而不是中断,抛出一个自定义异常,您可以在循环外捕获。如果在单次迭代中花费的时间相当多,它可以为您提供更好的精度,但是您可能会在每次迭代中失去一些需要完成的副作用。换句话说,你需要设计它。



另一种具有更高精度的方法可能是有一个单独的线程,你可以通过外部计时器中止,但这种方法是除非你真的非常了解这项技术并且非常小心,否则风险很大。



-SA
I cannot imagine why it can be useful; it sounds like a abuse to me.

Anyway, the best way to do it is using System.Diagnostics.Stopwatch. You start it before loop and check in loop and break it when it's time to break: Stopwatch Class (System.Diagnostics)[^].

Of course, the accuracy cannot be better than the time of a single iteration. You can also check up time several times in different part of the code of the iteration. Ultimately, if the code is highly structured, you can have checkup on different places and different levels and, instead of break, throw a custom exception, which you can catch outside the loop. If the time spend inside a single iteration is considerable, it can give you much better precision, but you may loose some side effect you would need to complete in each iteration. In other words, you need to design it.

Another approach with better accuracy could be having a separate thread which you could abort by an outside timer, but this approach is risky unless you really deeply understand the technology and are extremely careful.

—SA


试试这个。



try this.

using System;


namespace ConsoleApplication1
{


    class Program
    {

        static bool flag = true;
        static System.Timers.Timer timer = new System.Timers.Timer();
        static void Main(string[] args)
        {
            int seconds = 3; // seconds to run the loop
            timer.Interval = seconds * 1000;
            timer.Start();
            timer.Elapsed += timer_Elapsed;

            while (flag)
            {
                // your code.
            }

            Console.WriteLine("done");
            Console.ReadLine();


        }

        static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            flag = false;
            timer.Stop();
        }


    }
}


while(true)

{

Thread.Sleep(1500);

//代码

}
while(true)
{
Thread.Sleep(1500);
//Code
}


这篇关于2012年视觉工作室的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆