Jump to content

CPU intensive Script (HELP!)


Marlo
 Share

Recommended Posts

Hello peeps! once again i seek aid from you find chaps.

So i have made a Download manager as best to my ability, keeping it as simple as possible for my users whilst still trying to be functional. So what i wanted to do was create this download manager so the user could enter about 10 links at a time (each file could be upto 200mb's) and then the script would then re-open itself for each link given so if 10 links were entered then 10 new instances of my script would be opened, each downloading a single file. I did it this way because INetGet can only be run one at a time (which sucks).

Now obviously my problem is when running 10 or so .exe files is very CPU intensive!!! Now i either need to cut down my script CPU usage or do a complete rebuild of my app.

Heres my code:

#include <GUIConstantsEx.au3>
#include <GuiStatusBar.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <ProgressConstants.au3>
#include <EditConstants.au3>
#NoTrayIcon

Opt("GUIOnEventMode", 1)
If $CmdLine[0] > 0 Then
    _RunDownloader($CmdLine[1], $CmdLine[2], $CmdLine[3], $CmdLine[4])
EndIf

Dim $staMainParts[2] = [150, 250]
Const $IniFile = @AppDataDir & "\" & "RapidLoad.ini"
Global $X = 5, $Y = 5
$winMain = GUICreate("Rapidshare Downloader", 627, 325, -1, -1)
$txtLinks = GUICtrlCreateEdit("", 8, 66, 609, 201)
$Label1 = GUICtrlCreateLabel("Enter Links Below:", 9, 48, 92, 17)
$staMain = _GUICtrlStatusBar_Create($winMain)
    _GUICtrlStatusBar_SetParts($staMain, $staMainParts)
    _GUICtrlStatusBar_SetText($staMain, "Account: 106545", 0)
    _GUICtrlStatusBar_SetText($staMain, "Status: Active", 1)
$btnDownload = GUICtrlCreateButton("Download!", 528, 272, 91, 25, 0)
$txtSaveTo = GUICtrlCreateInput(@HomeDrive & "\", 8, 24, 529, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
$btnBrowse = GUICtrlCreateButton("Browse", 544, 22, 75, 25, 0)
$Label2 = GUICtrlCreateLabel("Save Location", 9, 6, 73, 17)
GUISetState(@SW_SHOW, $winMain)

GUICtrlSetData($txtSaveTo, IniRead($IniFile, "Settings", "SaveTo", @HomeDrive & "\"))

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit", $winMain)
GUICtrlSetOnEvent($btnBrowse, "_ChangeSaveTo")
GuiCtrlSetOnEvent($btnDownload, "_Download")

While 1
    
WEnd

Func _RunDownloader($Link, $SaveTo, $X, $Y)
    Dim $StaMainParts[3] = [50, 150, 150]
    Dim $Size = InetGetSize($Link)
    Dim $Bytes, $NewBytes, $BytesLeft, $N
    Global $Name = _GetName($Link)
    $winDownload = GUICreate("RapidLoader", 289, 83, $X, $Y, -1, $WS_EX_TOOLWINDOW)
    $prgDownload = GUICtrlCreateProgress(8, 32, 270, 17)
    $staMain = _GUICtrlStatusBar_Create($winDownload)
        _GUICtrlStatusBar_SetParts($staMain, $staMainParts)
        _GUICtrlStatusBar_SetText($staMain, "0%", 0)
        _GUICtrlStatusBar_SetText($staMain, "Speed: 0kb/s", 1)
        _GUICtrlStatusBar_SetText($staMain, "0 Megabytes Remaining", 2)
    $Label1 = GUICtrlCreateLabel("Downloading: ", 8, 8, 72, 17)
    $labName = GUICtrlCreateLabel($Name, 78, 8, -1, -1)
    GUISetState(@SW_SHOW)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_DownloadExit")
    
    InetGet($Link, $SaveTo & "\" & $Name, 1, 1)
    
    While @InetGetActive
        $BytesLeft = $Size - @InetGetBytesRead
        $Bytes = Round(@InetGetBytesRead)
        Sleep(1000)
        $NewBytes = Round(@InetGetBytesRead)
        $Percent = StringLeft(@InetGetBytesRead / $Size * 100, 4)
        GUICtrlSetData($prgDownload, $Percent)
        _GUICtrlStatusBar_SetParts($staMain, $staMainParts)
        _GUICtrlStatusBar_SetText($staMain, $Percent & "%", 0)
        _GUICtrlStatusBar_SetText($staMain, "Speed: " & ($NewBytes - $Bytes) /1024 & "kb/s", 1)
        _GUICtrlStatusBar_SetText($staMain, Int($BytesLeft / 1048576) & " Megabytes Remaining", 2)
        Sleep(7000)
    WEnd
    $N = MsgBox(68, "Download Complete", "The download of: " & @CRLF & $Name & @CRLF & "Has completed. Do you wish to open the file?")
    If $N = 6 Then
        ShellExecute($SaveTo & "\" & $Name)
    EndIf
    Exit
EndFunc

Func _Download()
    Dim $Text = GUICtrlRead($txtLinks)
    If $Text = "" Then
        MSgBox(48, "Error", "You did not enter any links!")
    Else
        Dim $Links = StringSplit($Text, @CR)
        For $I = 1 To Ubound($Links) -1
            _StartDownload($Links[$I])
            Sleep(500)
        Next
    EndIf
EndFunc

Func _StartDownload($Link)
    ShellExecute(@ScriptFullPath, $Link & ' "' & GUICtrlRead($txtSaveTo) & '" ' & $X & " " & $Y)
    If ($Y + 220) > @DesktopHeight Then
        $X += 300
        $Y = 5
    Else
        $Y += 110
    EndIf
EndFunc

Func _ChangeSaveTo()
    Dim $eFile = FileSelectFolder("Save files to...", "*", 7, @HomeDrive)
    If $eFile > "" Then
        GUICtrlSetData($txtSaveTo, $eFile)
    EndIf
EndFunc

Func _GetName($Link)
    Dim $N = StringSplit($Link, "/")
    Return $N[Ubound($N)-1]
EndFunc

Func _DownloadExit()
    Dim $N = MSgBox(36, "Cancel Download", 'Are you sure you want to cancel the download of "' & $Name & '" ?')
    If $N = 6 Then
        Exit
    EndIf
EndFunc

Func _Exit()
    IniWrite($IniFile, "Settings", "SaveTo", GUICtrlRead($txtSaveTo))
    Exit
EndFunc

If you look at the RunDownloader function i have a 7 second sleep time in my While loop. This cuts down the CPU usage but 7 seconds to update the KB/s sucks =(

Any ideas would be awesome.

Peace

Edited by Marlo
Click here for the best AutoIt help possible.Currently Working on: Autoit RAT
Link to comment
Share on other sites

didnt work =( one window takes up 50% of CPU

If I run your script it uses 50% of CPU time as you say.

If I put Sleep(50) in the while/wend loop it goes down to less than 1% (ie it shows 0)

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

didnt work =( one window takes up 50% of CPU

...the other loop

Why running 10 instances? Run one and load it with 10 scripts using "/AutoIt3ExecuteScript" (actually one but with 10 different parameters).

edit:

though running another script is another instance

Edited by trancexx
Link to comment
Share on other sites

/AutoIt3ExecuteScript requires autoit to be installed though and i want my app to be independent.

A compiled Au3 exe has theĀ /AutoIt3ExecuteScript ability also.

Your.exe /AutoIt3ExecuteScript <Autoit script (au3/a3x)>
Edited by Mobius

wtfpl-badge-1.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...