Sign in to follow this  
Followers 0
ken82m

High Cpu Usage After Converting Small Script

2 posts in this topic

I have a script I wrote in version 2 which simply monitored for some files I had it running as a service. It might jump up to 1% in task manager when it found a file and renamed it but otherwise it was always at 0.

I converted the same script to version 3 and now it's taking 25% of my cpu while doing nothing (just watching for the files). Any ideas? Here's my code.

V2 (Low CPU Usage):

SetEnv,PDFRoot,C:\\CreatePDF\\

SetEnv, SourceFile, CreatePDF

Setenv, Ext,.PSF

Loop:

SetEnv,Random,

SetEnv,Source,

SetEnv,Destination,

SetEnv, PrintSource, 0

IfExist, %PDFRoot%%SourceFile%1%Ext%, SetEnv, PrintSource, 1

IfExist, %PDFRoot%%SourceFile%2%Ext%, SetEnv, PrintSource, 2

IfExist, %PDFRoot%%SourceFile%3%Ext%, SetEnv, PrintSource, 3

IfExist, %PDFRoot%%SourceFile%4%Ext%, SetEnv, PrintSource, 4

IfExist, %PDFRoot%%SourceFile%5%Ext%, SetEnv, PrintSource, 5

IfExist, %PDFRoot%%SourceFile%6%Ext%, SetEnv, PrintSource, 6

IfExist, %PDFRoot%%SourceFile%7%Ext%, SetEnv, PrintSource, 7

IfExist, %PDFRoot%%SourceFile%8%Ext%, SetEnv, PrintSource, 8

IfExist, %PDFRoot%%SourceFile%9%Ext%, SetEnv, PrintSource, 9

IfExist, %PDFRoot%%SourceFile%10%Ext%, SetEnv, PrintSource, 10

IfNotEqual,PrintSource,0, Goto, Rename

goto, Loop

Rename:

Random, Random, 100, 900

Setenv, Source, ToConvert%Random%.PSF

Setenv, Destination, ConvertedPDF%Random%.PDF

Runwait, %comspec% /C "ren %PDFRoot%%SourceFile%%PrintSource%%Ext% %Source%", %systemroot%, Hide

FileAppend,%PDFRoot%X%Random%Y%Source%Z%Destination%\n,C:\\CreatePDF\\Queue.txt

goto, Loop

ENDPGM:

exit

V3 (High CPU Usage):

GLOBAL $PDFRoot

GLOBAL $SourceFile

$PDFRoot = "C:\CreatePDF\"

$SourceFile = "CreatePDF"

$Loop = 0

Do

If FileExists($PDFRoot & $SourceFile & "1.PSF") = 1 Then Rename(1)

If FileExists($PDFRoot & $SourceFile & "2.PSF") = 1 Then Rename(2)

If FileExists($PDFRoot & $SourceFile & "3.PSF") = 1 Then Rename(3)

If FileExists($PDFRoot & $SourceFile & "4.PSF") = 1 Then Rename(4)

If FileExists($PDFRoot & $SourceFile & "5.PSF") = 1 Then Rename(5)

If FileExists($PDFRoot & $SourceFile & "6.PSF") = 1 Then Rename(6)

If FileExists($PDFRoot & $SourceFile & "7.PSF") = 1 Then Rename(7)

If FileExists($PDFRoot & $SourceFile & "8.PSF") = 1 Then Rename(8)

If FileExists($PDFRoot & $SourceFile & "9.PSF") = 1 Then Rename(9)

If FileExists($PDFRoot & $SourceFile & "10.PSF") = 1 Then Rename(10)

Until $Loop = 1

Exit

Func Rename($1)

$Random = Random(100, 900)

$Source = $SourceFile & $1 & ".PSF"

$ToConvert = "ToConvert" & $Random & ".PSF"

$Converted = "ConvertedPDF" & $Random & ".PDF"

FileMove($PDFRoot & $Source, $PDFRoot & $ToConvert, 1)

$file = FileOpen($PDFRoot & "Queue.txt", 1)

FileWriteLine($File, $PDFRoot & "X" & $Random & "Y" & $ToConvert & "Z" & $Converted & @CR)

FileClose($File)

EndFunc


My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites



ahhhhh perfect thanks :whistle:

I'm actually monitoring for printed postsript files. It's not perfect sometimes if you print two small jobs too close to each other they overwrite but it works.

You don't know of anyway to monitor the port or the printer in windows? Something more efficient than just checking for files every 1ms B)


My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
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
Sign in to follow this  
Followers 0