新足迹

 找回密码
 注册

精华好帖回顾

· 圆头之旅 (2008-11-7) 仰望星空 · 老洋房里的那些记忆 (2008-10-19) UEJ
· 人生不来一次痛快的打架,是多么的遗憾. (2009-5-29) 仰望星空 · 分享自己今天刚做完的一个桌子和两个凳子(设计很独特) (2010-8-4) coleclark999
Advertisement
Advertisement
查看: 1746|回复: 15

程序中的时间控制 [复制链接]

发表于 2010-11-26 17:43 |显示全部楼层
此文章由 未名湖 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 未名湖 所有!转贴必须注明作者、出处和本声明,并保持内容完整
RT
个小程序,小语言,可能用的不多MQL4语言,类似C#,C++。。但希望知道大牛们,,用自己常用的语言,怎么实现的。

用控制程序中的时间间隔。即执行第一次后,在一定时间(20分钟或者5000秒)后再执行第二次。

MQL的程序大概如下:
int start()
  {
   double 。。。。。。;
   int    。。。。。。;
   int ();
// initial data checks
   if(....)
     {
      Print("..........");
      return(0);  
     }
   if(.....)
     {
      Print("........");
      return(0);  
     }
// to simplify the coding and speed up access
// data are put into internal variables
   _...........
   
   
   if(total<3)
     {
      ..................;
     ..................;   
   
      Sleep(10000000000) or wait(1000000);
      return(0);         
        }
      return(0);
     }
我想到的办法是用sleep或者wait等函数,设定值(milliseconds),等待或者睡眠一定之间再次执行红色点点的代码
还有一种是是,用函数读取系统时间,比较两次执行的时间差。当差到一定程度后,再执行。

不知道大牛们用自己的语言是怎么解决这种程序间隔再次的执行。谢谢赐教,小弟虚心学习。
Advertisement
Advertisement

发表于 2010-11-26 17:59 |显示全部楼层
此文章由 kawara 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 kawara 所有!转贴必须注明作者、出处和本声明,并保持内容完整
Cfd 真的能激发对编程的兴趣啊。

Sleep就可以

评分

参与人数 1积分 +1 收起 理由
未名湖 + 1 谢谢奉献

查看全部评分

发表于 2010-11-26 18:04 |显示全部楼层
此文章由 kawara 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 kawara 所有!转贴必须注明作者、出处和本声明,并保持内容完整
你也太扣了

评分

参与人数 1积分 +3 收起 理由
未名湖 + 3 哥,我总共就能加9分,还还等着感谢其他大牛们 ...

查看全部评分

发表于 2010-11-26 18:31 |显示全部楼层
此文章由 coin_king 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 coin_king 所有!转贴必须注明作者、出处和本声明,并保持内容完整
using System;
using System.Timers;

public class Timer1
{
    private static System.Timers.Timer aTimer;

    public static void Main()
    {


        // Create a timer with a ten second interval.
        aTimer = new System.Timers.Timer(10000);

        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        // Set the Interval to 2 seconds (2000 milliseconds).
        aTimer.Interval = 2000;
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program.");
        Console.ReadLine();

        // If the timer is declared in a long-running method, use
        // KeepAlive to prevent garbage collection from occurring
        // before the method ends.
        //GC.KeepAlive(aTimer);
    }

    // Specify what you want to happen when the Elapsed event is
    // raised.
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }
}

/* This code example produces output similar to the following:

Press the Enter key to exit the program.
The Elapsed event was raised at 5/20/2007 8:42:27 PM
The Elapsed event was raised at 5/20/2007 8:42:29 PM
The Elapsed event was raised at 5/20/2007 8:42:31 PM
...
*/

发表于 2010-11-26 18:40 |显示全部楼层

回复 4# 的帖子

此文章由 kawara 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 kawara 所有!转贴必须注明作者、出处和本声明,并保持内容完整
// If the timer is declared in a long-running method, use
        // KeepAlive to prevent garbage collection from occurring
        // before the method ends.
        //GC.KeepAlive(aTimer);

DotNEt真傻,被引用着的对象也会被垃圾回收

发表于 2010-11-26 18:48 |显示全部楼层
此文章由 coin_king 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 coin_king 所有!转贴必须注明作者、出处和本声明,并保持内容完整
原帖由 kawara 于 2010-11-26 19:40 发表
// If the timer is declared in a long-running method, use
        // KeepAlive to prevent garbage collection from occurring
        // before the method ends.
        //GC.KeepAlive(aTimer);

DotNEt真 ...

不要侮辱我们的衣食父母
Advertisement
Advertisement

发表于 2010-11-26 18:56 |显示全部楼层
此文章由 ericlgq 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 ericlgq 所有!转贴必须注明作者、出处和本声明,并保持内容完整
JAVA里边有Timer和 TimerTask 工具类来做这个。

        public static void main(String[] args)
        {
                TimerTask task = new TimerTask()
                {
                        @Override
                        public void run() {
                                System.out.println("......");
                               
                        }
                       
                };
               
                Timer timer = new Timer();
                timer.schedule(task, 1000);
        }

[ 本帖最后由 ericlgq 于 2010-11-26 20:22 编辑 ]

发表于 2010-11-26 19:01 |显示全部楼层
此文章由 coin_king 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 coin_king 所有!转贴必须注明作者、出处和本声明,并保持内容完整
还不给加分啊(monkey05)

评分

参与人数 1积分 +2 收起 理由
未名湖 + 2

查看全部评分

发表于 2010-11-26 19:38 |显示全部楼层
此文章由 未名湖 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 未名湖 所有!转贴必须注明作者、出处和本声明,并保持内容完整
原帖由 liedong 于 2010-11-26 20:01 发表
还不给加分啊(monkey05)



谢谢了,大牛,我刚吃完回来,分分马上补上

发表于 2010-11-26 21:16 |显示全部楼层

回复 5# 的帖子

此文章由 cdfei 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 cdfei 所有!转贴必须注明作者、出处和本声明,并保持内容完整
The purpose of the KeepAlive method is to ensure the existence of a reference to an object that is at risk of being prematurely reclaimed by the garbage collector. A common scenario where this might happen is when there are no references to the object in managed code or data, but the object is still in use in unmanaged code such as Win32 APIs, unmanaged DLLs, or methods using COM.

.NET不可能象你说的那么傻,毕竟这么多人在用。

评分

参与人数 1积分 +2 收起 理由
未名湖 + 2

查看全部评分

发表于 2010-11-26 21:21 |显示全部楼层

我是根据他comment out的code说的

此文章由 kawara 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 kawara 所有!转贴必须注明作者、出处和本声明,并保持内容完整
// If the timer is declared in a long-running method, use
        // KeepAlive to prevent garbage collection from occurring
        // before the method ends.

        //GC.KeepAlive(aTimer);

如果真是这样的话,也有一个好处,比较不容易out of memory

[ 本帖最后由 kawara 于 2010-11-26 22:22 编辑 ]

评分

参与人数 1积分 +2 收起 理由
未名湖 + 2

查看全部评分

Advertisement
Advertisement

退役斑竹

发表于 2010-11-26 21:29 |显示全部楼层
此文章由 大饼 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 大饼 所有!转贴必须注明作者、出处和本声明,并保持内容完整
这种长时间的等待还是用系统时间吧。
sleep可能不准的

评分

参与人数 1积分 +2 收起 理由
未名湖 + 2

查看全部评分

发表于 2010-11-28 13:00 |显示全部楼层
此文章由 未名湖 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 未名湖 所有!转贴必须注明作者、出处和本声明,并保持内容完整
原帖由 大饼 于 2010-11-26 22:29 发表
这种长时间的等待还是用系统时间吧。
sleep可能不准的



谢谢,我最终还是选用了两次读取系统时间,做一定差值的比较,来作为条件,短时间停止程序执行

可能是水平太次,用sleep或者wait等功能,总是不能让系统执行的很好,而且,有时间竟然让真个程序停止了(目的是部分模块暂停),

发表于 2010-11-28 21:50 |显示全部楼层
此文章由 乱码 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 乱码 所有!转贴必须注明作者、出处和本声明,并保持内容完整
原帖由 kawara 于 2010-11-26 22:21 发表
// If the timer is declared in a long-running method, use
        // KeepAlive to prevent garbage collection from occurring
        // before the method ends.
        //GC.KeepAlive(aTimer);

如果真是 ...


garbage collector一段时间就作一下stack walk,把没有被stack reference的在heap上的allocation回收一下,keepalive很subtle,在实际工作中倒不是最重要的,相反,在heap上的不用的object allocation如何让gc及时回收才是最重要的。java也应该差不多,印象中它什么data type都是reference type,没有value type.(correct me if i'm wrong)

特殊贡献奖章

发表于 2010-11-29 09:37 |显示全部楼层
此文章由 kr2000 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 kr2000 所有!转贴必须注明作者、出处和本声明,并保持内容完整
可以用cronjob
这个问题可以把计算的结果存在一个地方
每20分钟运行一次,读取存上次运行的结果,再根据条件运行后一部分

2010年度奖章获得者

发表于 2010-11-29 09:40 |显示全部楼层
此文章由 dalaohu 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 dalaohu 所有!转贴必须注明作者、出处和本声明,并保持内容完整
看情况了,一般程序自己不要设timer。

把logic  decouple 出来, 服务器上做个windows service 来call他。
Advertisement
Advertisement

发表回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Advertisement
Advertisement
返回顶部