Solved

Get program to slow down

Posted on 2002-05-03
8
161 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:amit_panjwani
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

772 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