?
Solved

Get program to slow down

Posted on 2002-05-03
8
Medium Priority
?
175 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:robjohnston
8 Comments
 
LVL 4

Expert Comment

by:Glowman
ID: 6989065
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
0
 
LVL 1

Expert Comment

by:darthg8r
ID: 6989066
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.
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 400 total points
ID: 6989068
in your loop add:
DoEvents (you can add more than one)

ie)

for i = 0 to 100000
   '' some code
   DoEvents
   DoEvents
   DoEvents
next
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:mcoop
ID: 6989635
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
0
 
LVL 4

Expert Comment

by:amit_panjwani
ID: 6989689
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
0
 
LVL 14

Expert Comment

by:Matti
ID: 6990623
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



0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6991483
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.
0
 
LVL 1

Author Comment

by:robjohnston
ID: 7018175
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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month16 days, 16 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question