Link to home
Start Free TrialLog in
Avatar of robjohnston
robjohnston

asked on

Get program to slow down

Hi folks.

I have written a backup program which I happy with appart from when it runs it ties the machine up.
I mean when it's copying files it uses a lot of resources. It also becomes unresponsive until the copy process has finished.

Is there a way I can get it chill out a little. I'd like it to be able to run in the background really, letting the user get on with other things while it's working.

Cheers, Rob.
Avatar of Glowman
Glowman
Flag of United States of America image

I have had some luck using the sleep() function to release some of the processor back to other processes.  I use about 15 for the sleep()  so its ( syn: Sleep(15))  Good luck hope it helps
Avatar of darthg8r
darthg8r

If your running WinNT, you could run it as a service.  When an app runs as a service, it's priority level becomes lower.  Running an app as a service can get very complicated, I suggest using a 3rd party control.  www.dart.com has a very good one.
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or - it may be more relevant to distribute the DoEvents throughout the code...

while / for / do etc

 : some code
 DoEvents

 : more code
 DoEvents

 : other code
 DoEvents

wend / next / loop etc
Alternatively,

In case you care using Windows NT
you can also set process priority

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function SetPriorityClass Lib "kernel32" (ByVal hProcess As Long, ByVal dwPriorityClass As Long) As Long


'Boost/Lower  Priority
    Dim mLong As Long
    mLong = SetPriorityClass(ByVal GetCurrentProcess, ByVal &H80)

Here I am setting it to Highest.


It would be lot informative if you could hint what algorithm / OS and Backup Media you are using and System Config

Amit
Hi!

Basicly if it need to responce to available processor time simplest way:

Do While DoEvents
Call FileCopy(MyFile, MyFile2)
Loop

The copy routine, is it compression or slow media to write, basicly need to solve why it need's so much resorces.
If you do copy a file using Windows Explorer, does it use similar "lot's" resources or not.

If so best is wait until one function call is executed before resume next.


Matti



If you are copying large files, doevents will not directly help since the copy occurs at the Windows level and returns to your code when it's done.  For small files, you won't notice, but for large files, you may wait a while until Windows responds.  This seems to be true regardless of how you perform the copy with one exception:  copying the file through code in small chunks.  To do that, try something like this:

Private sub CopyFile(SourceFileName as string, DestinationFileName as string, Optional DeleteOriginal as boolean = false)
  on error goto CopyFile_Err

  dim iFileNumberIn as integer
  dim iFileNumberOut as integer
  dim strDataBlock as string
  dim lLoopCount as long

  open SourceFileName for binary as #iFileNumberIn
  open DestinationFileName for output as #iFileNumberOut

  ' Transfer blocks of 32000 characters at a time
  lLoopCount = lof(iFileNumberIn)
  while lLoopCount > 0 then
    if lLoopCount > 32000 then
      strDataBlock = input$(32000, iFileNumberIn)
      lLoopCount =lLoopCount - 32000
    else
      strDataBlock = input$(lLoopCount, iFileNumberIn)
      lLoopCount = 0
    endif
    print #iFileNumberOut, strDataBlock;
    doevents
  wend

  close #iFileNumberOut
  close #iFileNumberIn

  if DeleteOriginal then
    kill iFileNumberIn
  endif
  exit sub
CopyFile_Err:
  close iFileNumberIn
  close iFileNumberOut
  msgbox "Error occurred while copying: " & err.number & " " & err.description
end sub

'
'----------------
' To use this, call the procedure with the path to the file you want to copy, and the path to the location where you want it to go.
' Note that I have not had a chance to test the above, so make sure you give it a good test before using it in production.  You may also want to add additional error-checking routines.
Avatar of robjohnston

ASKER

Hi folks.

Most sorry about the delay in getting back to this question and thank you all for you time and help.

The do events was what I needed to use, that gave the app time to breath. The app is copying the files to a CD using direct CD so that part is slow anyway but the do events has made a big difference.

Thanks for everybodies help, Rob.