我做了一个程序:判断时间到10:05:00定时启动执行后面的代码,当天能实现这种功能,可到从第二天开始就不管用了。
请问是怎么回事?
怎么解决?
是不是要用到多线程?偶不懂现在是用Windows的任务功能每天晚上12点前自动把它关闭,第二天0点以后再启动,可是这样带来很多不便。有没有代码在程序里就能解决的?您可以做一个定时写文件的作例子。小弟万分感谢!!!

解决方案 »

  1.   

    我编写的是一个自动压缩程序源程序如下:
    (通用)(声明)
    Option Explicit
      Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal lpszPath As String) As Long
    Private Sub Timer1_Timer()
     Dim a As String
     Dim s As String
     Dim yesth01, yestm01, yesth04, yestm04, rarname, rarsearch As String   'Variant
         
        '查找文件是否存在
       ' If Mid(Format(Now, "YYYYMMDDHHMMSS"), 9) = rarsearch Then   '20:33:59定时执行 
           a = Mid(Format(Now, "YYYYMMDD"), 1) & ".h04"
        
     
              If PathFileExists("D:\yjm\" & a) Then
                     
                   '如果今日文件存在则将昨日的四个文件压缩
                     
                   yesth01 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".h01"
                   yestm01 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".m01"
                   yesth04 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".h04"
                   yestm04 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".m04"
            
                   rarname = Mid(Format(Now, "YYYYMMDD"), 1) & "QZ05A"
                   s = "C:\Program Files\WinRAR\WinRAR.exe "
                   s = s & "a d:\\yjm\\" & rarname & ".rar "
        
                   '将昨日四个数据文件添加至压缩文件内
                   s = s & yesth01      '添加昨天的H01文件
                   s = s & yestm01      '添加昨天的M01文件
                   s = s & yesth04      '添加昨天的H04文件
                   s = s & yestm04      '添加昨天的M04文件
       
                   Shell s              '调用压缩文件,将昨日四个文件压缩
                   
                   Timer1.Enabled = False   '将timer1的属性设置为false 
                   
             ' Else
                     ' MsgBox "文件未找到,请手动压缩!"
             ' End If  End If注:timer的interval属性是1000关键问题可能在这儿,将timer1的属性设置为false可是如果不将其设为false会一直在自动压缩啊???求救!
      

  2.   

    在Private Sub Timer1_Timer()里判断系统时间,如果到时间了才运行程序,否则不运行 
      

  3.   

    Option Explicit 
      Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal lpszPath As String) As Long 
    Private Sub Timer1_Timer() 
    Dim a As String 
    Dim s As String 
    Dim yesth01, yestm01, yesth04, yestm04, rarname, rarsearch As String  'Variant 
        
        '查找文件是否存在 
       If Format(Now, "YYYYMMDDHHMMSS") > rarsearch Then  '20:33:59定时执行 不仅时间        rarsearch=Format(dateadd("d",1,Now), "YYYYMMDDHHMMSS")       a = Mid(Format(Now, "YYYYMMDD"), 1) & ".h04" 
                  If PathFileExists("D:\yjm\" & a) Then 
                    
                  '如果今日文件存在则将昨日的四个文件压缩 
                    
                  yesth01 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".h01" 
                  yestm01 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".m01" 
                  yesth04 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".h04" 
                  yestm04 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".m04" 
            
                  rarname = Mid(Format(Now, "YYYYMMDD"), 1) & "QZ05A" 
                  s = "C:\Program Files\WinRAR\WinRAR.exe " 
                  s = s & "a d:\\yjm\\" & rarname & ".rar " 
        
                  '将昨日四个数据文件添加至压缩文件内 
                  s = s & yesth01      '添加昨天的H01文件 
                  s = s & yestm01      '添加昨天的M01文件 
                  s = s & yesth04      '添加昨天的H04文件 
                  s = s & yestm04      '添加昨天的M04文件 
      
                  Shell s              '调用压缩文件,将昨日四个文件压缩               '去掉
                  'Timer1.Enabled = False  '将timer1的属性设置为false 

                  
            ' Else 
                    ' MsgBox "文件未找到,请手动压缩!" 
             End If   End If 
      

  4.   

    Option Explicit
    Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal lpszPath As String) As LongPrivate m_NextTime  As Long '下次执行时间Private Sub Form_Load()
        m_NextTime = Date + #8:33:59 PM#
        If Now() > m_NextTime Then
            m_NextTime = DateAdd("d", 1, m_NextTime)
        End If
        
        Timer1.Enabled = True
    End SubPrivate Sub Timer1_Timer()
        Dim a As String
        Dim s As String
        Dim yesth01, yestm01, yesth04, yestm04, rarname, rarsearch As String  'Variant
        If Now > m_NextTime Then
            '查找文件是否存在
            a = Mid(Format(Now, "YYYYMMDD"), 1) & ".h04"
            If PathFileExists("D:\yjm\" & a) Then
                Timer1.Enabled = False            '如果今日文件存在则将昨日的四个文件压缩            yesth01 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".h01"
                yestm01 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".m01"
                yesth04 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".h04"
                yestm04 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".m04"            rarname = Mid(Format(Now, "YYYYMMDD"), 1) & "QZ05A"
                s = "C:\Program Files\WinRAR\WinRAR.exe "
                s = s & "a d:\\yjm\\" & rarname & ".rar "            '将昨日四个数据文件添加至压缩文件内
                s = s & yesth01      '添加昨天的H01文件
                s = s & yestm01      '添加昨天的M01文件
                s = s & yesth04      '添加昨天的H04文件
                s = s & yestm04      '添加昨天的M04文件            Shell s              '调用压缩文件,将昨日四个文件压缩            m_NextTime = DateAdd("d", 1, m_NextTime)  '<-执行时间改为下一天
                Timer1.Enabled = True
            End If
        End If
    End Sub
      

  5.   

    关开时间的操作个人觉得不太好!时间间隔1秒 时间事件代码:if 系统时间=规定时间 then 执行特定代码======================
    规定的时间1天只有1次相同  一直开着时间 让它判断时间到就执行 这样不就可以了么!
      

  6.   

    Option Explicit 
      Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal lpszPath As String) As Long 
    Private Sub Timer1_Timer() 
    Static bIsExe As Boolean '判断是否已执行
    Dim a As String 
    Dim s As String 
    Dim yesth01, yestm01, yesth04, yestm04, rarname, rarsearch As String  'Variant 
        
        '查找文件是否存在 
      ' If Mid(Format(Now, "YYYYMMDDHHMMSS"), 9) > rarsearch and bIsExe =False Then  '20:33:59定时执行 
          a = Mid(Format(Now, "YYYYMMDD"), 1) & ".h04" 
                  If PathFileExists("D:\yjm\" & a) Then 
                    
                  '如果今日文件存在则将昨日的四个文件压缩 
                    
                  yesth01 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".h01" 
                  yestm01 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".m01" 
                  yesth04 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".h04" 
                  yestm04 = " D:\\yjm\\" & Format$(DateAdd("d", -1, Date), "yyyymmdd") & ".m04" 
            
                  rarname = Mid(Format(Now, "YYYYMMDD"), 1) & "QZ05A" 
                  s = "C:\Program Files\WinRAR\WinRAR.exe " 
                  s = s & "a d:\\yjm\\" & rarname & ".rar " 
        
                  '将昨日四个数据文件添加至压缩文件内 
                  s = s & yesth01      '添加昨天的H01文件 
                  s = s & yestm01      '添加昨天的M01文件 
                  s = s & yesth04      '添加昨天的H04文件 
                  s = s & yestm04      '添加昨天的M04文件 
      
                  Shell s              '调用压缩文件,将昨日四个文件压缩 
                  '去掉
                  'Timer1.Enabled = False  '将timer1的属性设置为false 
                bIsExe=True
                  
            ' Else 
                    ' MsgBox "文件未找到,请手动压缩!" 
            ' End If   End If