Jump to content
Sign in to follow this  
rusticdog

Progress Bar, Sleeps and REGEDIT

Recommended Posts

rusticdog

I have a small backup and restore script that was working fine, I went to add a progress bar so that it was obvious the program had not hung, but found the script runs so quickly (at least on my machine) the progress bar was not updating, it would go from a 20% state straight to the completed message.

So I thought I'd just add in some small .1 second sleeps between steps, however having done that I am suddenly finding I'm getting errors about the REGEDIT command which exports data to a file.

"Cannot export C:\MWP_backup\firetrust.reg: Error opening the file. There may be a disk or file system error."

The moment I take the Sleeps out, it works fine again. Here is the script that fails, removing the 5 Sleep(100) will seemingly make it work again.

The most obvious solution is to remove the sleeps and just forget about it, but as this strikes me as very odd, it is something I would like to at least understand a little more.

Any help appreciated

#requireadmin
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>


Opt("GUIOnEventMode", 1); Change to OnEvent mode
$mainwindow = GUICreate("MailWasher Pro backup and restore utility", 200, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked")
$backupText = GUICtrlCreateLabel("Click the BackUp button to begin", 30, 10)
$backupButton = GUICtrlCreateButton("BackUp", 70, 50, 60)
GUICtrlSetOnEvent($backupButton, "backupButton")
$restoreButton = GUICtrlCreateButton("Restore", 70, 100, 60)
GUICtrlSetOnEvent($restoreButton, "restoreButton")
$closeButton = GUICtrlCreateButton("Exit", 70, 150, 60)
GUICtrlSetOnEvent($closeButton, "CLOSEClicked")
GUISetState(@SW_SHOW)

While 1
  Sleep(1000); Idle around
WEnd

Func backupButton()
;Note: at this point @GUI_CTRLID would equal $okbutton,
;and @GUI_WINHANDLE would equal $mainwindow
  
    GUICtrlSetState($backupText, $GUI_HIDE)
    GUICtrlSetState($backupButton, $GUI_HIDE)
    GUICtrlSetState($restoreButton, $GUI_HIDE)
    GUICtrlSetState($closeButton, $GUI_HIDE)

$progressText = GUICtrlCreateLabel("Backing up data", 30, 10)
$progressBar = GUICtrlCreateProgress(5,65,190,30)


    $PID = ProcessExists("mailwasher.exe") 
    If $PID Then ProcessClose($PID)
    ProcessWaitClose("mailwasher.exe")
    
GUICtrlSetData($progressBar, 20)
Sleep(100)
    
    DirRemove("C:\MWP_backup_old", 1)
    
GUICtrlSetData($progressBar, 40)
Sleep(100)
    
    DirMove("C:\MWP_backup", "C:\MWP_backup_old", 1)
    
GUICtrlSetData($progressBar, 60)
Sleep(100)
    
    Run("C:\windows\regedit.exe /E C:\MWP_backup\firetrust.reg HKEY_CURRENT_USER\Software\FireTrust")
    
GUICtrlSetData($progressBar, 80)
Sleep(100)
    
    If @OSVersion = 'WIN_VISTA' Then 
        DirCopy(@UserProfileDir & "\AppData\Roaming\MailWasherPro", "C:\MWP_backup\App Data", 1)
    Else
        DirCopy(@UserProfileDir & "\Application Data\MailWasher Pro", "C:\MWP_backup\App Data", 1)
    EndIf

GUICtrlSetData($progressBar, 100)
Sleep(100)

    GUICtrlSetState($progressBar, $GUI_HIDE)
    GUICtrlSetState($progressText, $GUI_HIDE)
    GUICtrlSetState($backupButton, $GUI_SHOW)
    GUICtrlSetState($restoreButton, $GUI_SHOW)
    GUICtrlSetState($closeButton, $GUI_SHOW)
    
    MsgBox(64, 'Mailwasher Pro', 'Backup is complete, contents have been saved to C:\MWP_backup')
    
EndFunc

Func restoreButton()
;Note: at this point @GUI_CTRLID would equal $okbutton,
;and @GUI_WINHANDLE would equal $mainwindow

    $PID = ProcessExists("mailwasher.exe") 
    If $PID Then ProcessClose($PID)
    ProcessWaitClose("mailwasher.exe")
    
    Run("C:\windows\regedit /S c:\MWP_backup\firetrust.reg")
    
    If @OSVersion = 'WIN_VISTA' Then
        DirCopy("C:\MWP_backup\App Data", @UserProfileDir & "\AppData\Roaming\MailWasherPro", 1)
    Else
        DirCopy("C:\MWP_backup\App Data", @UserProfileDir & "\Application Data\MailWasher Pro", 1)
    EndIf
        
    MsgBox(64, 'Mailwasher Pro', 'Restore is complete')
    
EndFunc

Func CLOSEClicked()
;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE, 
;and @GUI_WINHANDLE would equal $mainwindow 
  Exit
EndFunc
Edited by rusticdog

Share this post


Link to post
Share on other sites
PsaltyDS

I have a small backup and restore script that was working fine, I went to add a progress bar so that it was obvious the program had not hung, but found the script runs so quickly (at least on my machine) the progress bar was not updating, it would go from a 20% state straight to the completed message.

So I thought I'd just add in some small .1 second sleeps between steps, however having done that I am suddenly finding I'm getting errors about the REGEDIT command which exports data to a file.

"Cannot export C:\MWP_backup\firetrust.reg: Error opening the file. There may be a disk or file system error."

The moment I take the Sleeps out, it works fine again. Here is the script that fails, removing the 5 Sleep(100) will seemingly make it work again.

The most obvious solution is to remove the sleeps and just forget about it, but as this strikes me as very odd, it is something I would like to at least understand a little more.

Any help appreciated

What if you remove all the Sleep() functions, except the ones right before and after the Reg.exe command? Then if you leave only the one before or after? Exactly which Sleep() leads to the error?

:)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
rusticdog

What if you remove all the Sleep() functions, except the ones right before and after the Reg.exe command? Then if you leave only the one before or after? Exactly which Sleep() leads to the error?

:)

Duh, I really ought to have done that first >_<

It is seemingly the Sleep after the REGEDIT command.

So it fails if I place it right after the REGEDIT, or right after the GUICtrlSetData($progressBar, 80) which immediately follows the REGEDIT.

Or if I place any Sleeps within the If @OSVersion statement it also fails.

Everything else seems fine though.

Share this post


Link to post
Share on other sites
PsaltyDS

Duh, I really ought to have done that first >_<

It is seemingly the Sleep after the REGEDIT command.

So it fails if I place it right after the REGEDIT, or right after the GUICtrlSetData($progressBar, 80) which immediately follows the REGEDIT.

Or if I place any Sleeps within the If @OSVersion statement it also fails.

Everything else seems fine though.

OK, so it works without the Sleep() in that section, but not with it. I think you have a race condition with your DirCopy() functions that follow this section. The REGEDIT export is opening a file in that directory for write, which means it can't be copied. REGEDIT is slow, because it has to load, open the registry, find the key you are exporting, and only then does it open a file for write. Without the Sleep(), your DirCopy() gets started before REGEDIT creates an open file handle in that space. With the Sleep(), REGEDIT has an open file handle in that space when you try to do your DirCopy().

Regardless of whether it hangs your script or not, this is obviously a bad design. You should be completing your REGEDIT export before copying the directory that it is exporting to. Change Run() to RunWait() on the REGEDIT, or do some other kind of loop to verify REGEDIT is done before going on.

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
rusticdog

Ah great, I've only just started with AutoIt and assumed Run did wait for commands to compete. Thanks for your help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×