曼菲士 发表于 2011-12-22 11:30:16

这方法是从P版代码里找出来的,我的程序需要连续至少8小时以上稳定运行,但经常十来分钟就自己退出,后来我在我的代码中用了这个方法,就没再自己退出过,每天从早到晚9小时运行都很稳定。

曼菲士 发表于 2011-12-22 12:42:01

你的代码错了,应该这么改

#cs ____________________________________

#ce _______________脚本开始_________________

#include <FTPEx.au3>
#include "CoProc.au3"
#include <misc.au3>
$iPidotherfuc = _CoProc("ftp")
While 1
      ;ftp()
      Sleep(10000)
#CS         If ProcessExists($iPidotherfuc) Then
                   ProcessClose($iPidotherfuc)
         endif      
#CE
WEnd      

Func ftp()      
      _Singleton("ftpfunc",0)
      Dim $downftpfileflag
      Dim $fFailIfExists
      ;$server = ''
      $server = ''
      $username = 's1'
      $pass = 'fin23'
      
      $Open = _FTP_Open('SlipFinder ftp ')
      $Conn = _FTP_Connect($Open, $server, $username, $pass)               
      ;MsgBox(0,"$Conn",_FTP_DirGetCurrent($Conn),1)
      If $Conn <> 0 Then               
                $aFile = _FTP_ListToArray($Conn, 2)                ;只找文件
                If IsArray($aFile) Then
                        Sleep(2000)                        ;等待下,以防止机台产生数据中                        
                        For $i= 1 To $aFile      
                              ;MsgBox(0,"",$aFile[$i],1)
                              $sourcefile = "/" & $aFile[$i]               
                              $targetfile = "C:\Slip\" & $aFile[$i]               
                              ;$filegroup=StringSplit($aFile[$i],".")
                              ;MsgBox(0,"",$filegroup)
                              ;If $filegroup = "txt" Then                        ;如果是txt档,则下载此文件
                                        $downftpfileflag=_FTP_FileGet($Conn,$sourcefile,$targetfile, $fFailIfExists = False,0,$INTERNET_FLAG_TRANSFER_ASCII)      ;false为不覆盖
                                        ;MsgBox(0,"上传标志",$downftpfileflag ,1)
                                        If $downftpfileflag = 1 Then               ;判断是否下载成功,成功即可删除
                                                _FTP_FileDelete($Conn, "/" & $aFile[$i])
                                                ;MsgBox(0,"文件夹名",$filegroup)
                                                ;_FTPRemovedir($Conn, "/" & $filegroup) ; 删除产生的文件夹
                                                ;MsgBox(0,"","上传后删除啦",1)
                                        Else                              ;如果下载不成功,
                                                ;MsgBox(0,"@error",@error,1)
                                                ;$downftpfileflag=_FTP_FileGet($Conn,"/" & $aFile[$i] ,"C:\" & $aFile[$i], $fFailIfExists = False)      
                                        EndIf
                              ;Else                                       ;如果不是txt档,则直接删除
                                        ;_FTP_FileDelete($Conn, "/" & $aFile[$i])
                                        ;MsgBox(0,"","直接删除啦",1)
                              ;EndIf                              
                        Next                        
                EndIf                        
      EndIf      
      $Ftpc = _FTP_Close($Open)
EndFunc

Func _FTPRemovedir($l_FTPSession, $sFTPDirectory)
    Local $aFile, $hSearch, $sWorkingdir, $sFolderlist, $i, $bFirst, $aFolderStack =
    While $aFolderStack > 0
      $sWorkingdir = $aFolderStack[$aFolderStack]
      $aFolderStack -= 1
      $aFile = _FTP_FindFileFirst($l_FTPSession, $sWorkingdir & '/*', $hSearch, $INTERNET_FLAG_NO_CACHE_WRITE)
      If Not @error Then
            $bFirst = True
            While 1
                If $bFirst = False Then
                  $aFile = _FTP_FindFileNext($hSearch)
                  If @error Then ExitLoop
                EndIf
                If $aFile = 16 Then; If file is a directory we are going to add it to the stack of folders we need to go through
                  $aFolderStack += 1
                  If UBound($aFolderStack) <= $aFolderStack Then ReDim $aFolderStack
                  $aFolderStack[$aFolderStack] = $sWorkingdir & "/" & $aFile
                  $sFolderlist &= $sWorkingdir & "/" & $aFile & ';'; Here I am adding the folder to a list of directorys I need to delete later
                Else; else we delete it
                  _FTP_FileDelete($l_FTPSession, $sWorkingdir & "/" & $aFile)
                  ConsoleWrite('File: ' & $sWorkingdir & "/" & $aFile & ' Deleted' & @CRLF)
                EndIf
                $bFirst = False
            WEnd
      EndIf
      _FTP_FindFileClose($hSearch)
    WEnd
    $aFolderStack = StringSplit(StringTrimRight($sFolderlist, 1), ';')
    For $i = $aFolderStack To 1 Step -1 ;Here we delete all those empty folders from last to first
      _FTP_DirDelete($l_FTPSession, $aFolderStack[$i])
      ConsoleWrite('Directory: ' & $aFolderStack[$i] & ' Deleted' & @CRLF)
    Next
    _FTP_DirDelete($l_FTPSession, $sFTPDirectory);Delete the original directory
   EndFunc   ;==>_FTPRemovedir

曼菲士 发表于 2011-12-22 12:46:38

$iPidotherfuc = _CoProc("ftp")

这行不能放在循环里,久了确实会出现问题。

leon460 发表于 2011-12-22 21:51:30

回复 18# 曼菲士


但是我要它不断的运行ftp()这个函数哦,,放在外面不是只能运行一次了?

曼菲士 发表于 2011-12-23 00:28:28

#cs ____________________________________

#ce _______________脚本开始_________________

#include <FTPEx.au3>
#include "CoProc.au3"
#include <misc.au3>
$iPidotherfuc = _CoProc("_ftp")
While 1
      ;ftp()
      Sleep(500)
#CS         If ProcessExists($iPidotherfuc) Then
                   ProcessClose($iPidotherfuc)
         endif      
#CE
WEnd      


func _ftp()
while 1
   ftp()
   Sleep(10000)
wend
endfunc

Func ftp()      
      _Singleton("ftpfunc",0)
      Dim $downftpfileflag
      Dim $fFailIfExists
      ;$server = ''
      $server = ''
      $username = 's1'
      $pass = 'fin23'
      
      $Open = _FTP_Open('SlipFinder ftp ')
      $Conn = _FTP_Connect($Open, $server, $username, $pass)               
      ;MsgBox(0,"$Conn",_FTP_DirGetCurrent($Conn),1)
      If $Conn <> 0 Then               
                $aFile = _FTP_ListToArray($Conn, 2)                ;只找文件
                If IsArray($aFile) Then
                        Sleep(2000)                        ;等待下,以防止机台产生数据中                        
                        For $i= 1 To $aFile      
                              ;MsgBox(0,"",$aFile[$i],1)
                              $sourcefile = "/" & $aFile[$i]               
                              $targetfile = "C:\Slip\" & $aFile[$i]               
                              ;$filegroup=StringSplit($aFile[$i],".")
                              ;MsgBox(0,"",$filegroup)
                              ;If $filegroup = "txt" Then                        ;如果是txt档,则下载此文件
                                        $downftpfileflag=_FTP_FileGet($Conn,$sourcefile,$targetfile, $fFailIfExists = False,0,$INTERNET_FLAG_TRANSFER_ASCII)      ;false为不覆盖
                                        ;MsgBox(0,"上传标志",$downftpfileflag ,1)
                                        If $downftpfileflag = 1 Then               ;判断是否下载成功,成功即可删除
                                                _FTP_FileDelete($Conn, "/" & $aFile[$i])
                                                ;MsgBox(0,"文件夹名",$filegroup)
                                                ;_FTPRemovedir($Conn, "/" & $filegroup) ; 删除产生的文件夹
                                                ;MsgBox(0,"","上传后删除啦",1)
                                        Else                              ;如果下载不成功,
                                                ;MsgBox(0,"@error",@error,1)
                                                ;$downftpfileflag=_FTP_FileGet($Conn,"/" & $aFile[$i] ,"C:\" & $aFile[$i], $fFailIfExists = False)      
                                        EndIf
                              ;Else                                       ;如果不是txt档,则直接删除
                                        ;_FTP_FileDelete($Conn, "/" & $aFile[$i])
                                        ;MsgBox(0,"","直接删除啦",1)
                              ;EndIf                              
                        Next                        
                EndIf                        
      EndIf      
      $Ftpc = _FTP_Close($Open)
EndFunc

Func _FTPRemovedir($l_FTPSession, $sFTPDirectory)
    Local $aFile, $hSearch, $sWorkingdir, $sFolderlist, $i, $bFirst, $aFolderStack =
    While $aFolderStack > 0
      $sWorkingdir = $aFolderStack[$aFolderStack]
      $aFolderStack -= 1
      $aFile = _FTP_FindFileFirst($l_FTPSession, $sWorkingdir & '/*', $hSearch, $INTERNET_FLAG_NO_CACHE_WRITE)
      If Not @error Then
            $bFirst = True
            While 1
                If $bFirst = False Then
                  $aFile = _FTP_FindFileNext($hSearch)
                  If @error Then ExitLoop
                EndIf
                If $aFile = 16 Then; If file is a directory we are going to add it to the stack of folders we need to go through
                  $aFolderStack += 1
                  If UBound($aFolderStack) <= $aFolderStack Then ReDim $aFolderStack
                  $aFolderStack[$aFolderStack] = $sWorkingdir & "/" & $aFile
                  $sFolderlist &= $sWorkingdir & "/" & $aFile & ';'; Here I am adding the folder to a list of directorys I need to delete later
                Else; else we delete it
                  _FTP_FileDelete($l_FTPSession, $sWorkingdir & "/" & $aFile)
                  ConsoleWrite('File: ' & $sWorkingdir & "/" & $aFile & ' Deleted' & @CRLF)
                EndIf
                $bFirst = False
            WEnd
      EndIf
      _FTP_FindFileClose($hSearch)
    WEnd
    $aFolderStack = StringSplit(StringTrimRight($sFolderlist, 1), ';')
    For $i = $aFolderStack To 1 Step -1 ;Here we delete all those empty folders from last to first
      _FTP_DirDelete($l_FTPSession, $aFolderStack[$i])
      ConsoleWrite('Directory: ' & $aFolderStack[$i] & ' Deleted' & @CRLF)
    Next
    _FTP_DirDelete($l_FTPSession, $sFTPDirectory);Delete the original directory
   EndFunc   ;==>_FTPRemovedir

你要会变更啊,其实很容易理解的.

leon460 发表于 2011-12-23 09:48:44

回复 20# 曼菲士

你的代码不就等同于以下?
While 1
      ftp()
      Sleep(1000)
WEnd      

func ftp()
endfunc

曼菲士 发表于 2011-12-23 10:50:48

回复 21# leon460


    无语了,楼主对自己程序的问题还没有认识,我发的代码只是一些提醒,至于怎么变通还是由楼主自己来改。
首先你用$iPidotherfuc = _CoProc("ftp")不停的执行来达到子程序长时间运行,这点本身就是错误的,你不知道_CoProc运行时会分配内存吗?长时间这就出现了你发图说的问题,内存分配错误,可能内存都分到没了,为了解决这个问题,你需要只运行一次$iPidotherfuc = _CoProc("ftp"),但又不能让ftp()这个子进程退出,就只有修改你的程序了,你不一定按我的代码来写,你可以把循环放到你的ftp()里,我只是给你点提醒,代码还得由你自己来改啊,大哥,让你成为伸手党的话,那就是给你现成的人害了你,编程需要积累经验,需要灵活变通,希望楼主能更加进步。

leon460 发表于 2011-12-23 11:27:18

本帖最后由 leon460 于 2011-12-23 11:32 编辑

回复 22# 曼菲士

感谢您的帮助,呵呵,
不知道针对_CoProc回收的内存的函数没,暂时在CoProc.au3 没有找到相关函数
页: 1 [2]
查看完整版本: 程序长时间运行出现error allocating memory错误