Solved

Get program to slow down

Posted on 2002-05-03
8
162 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now