Solved

Get program to slow down

Posted on 2002-05-03
8
166 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 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
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!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

761 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