4 Replies - 12410 Views - Last Post: 25 October 2010 - 12:09 PM Rate Topic: -----

#1 jellever  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 25-October 10

can't delete file, file in use

Posted 25 October 2010 - 04:59 AM

Hi all,

I have a problem with the myProcess.WaitForExit(). I have this piece of code here:

download_update(get_download_url("ccleaner", build.ToLower), Application.StartupPath + "/ccsetup" + new_version.ToString + ".exe")

Dim myProcess As Process = System.Diagnostics.Process.Start(Application.StartupPath + "/ccsetup" + new_version.ToString + ".exe", "/S")

myProcess.WaitForExit()
                    
remove_file(Application.StartupPath + "/ccsetup" + new_version.ToString + ".exe")



I start the downloaded update with myprocess and wait for it to exit. Then i want to remove the update file. On my system everything goes fine but on my xp machine(slow pc) it sometimes lockes up. I get an error: can't delete file because the file is in use. I have tried a sleep function for 1 second after the waitforexit(). This seems to reduce the times it locked up but it is still there sometimes..

How do i solve this? Write a custom function to check if file is really closed or..?

thanks in advance

Jelle

Is This A Good Question/Topic? 0
  • +

Replies To: can't delete file, file in use

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: can't delete file, file in use

Posted 25 October 2010 - 05:15 AM

How about this: put the file removal in a loop, catch the exception, wait a few seconds, and try again? Do that for up to how ever many tries you want to attempt.
Was This Post Helpful? 0
  • +
  • -

#3 jellever  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 25-October 10

Re: can't delete file, file in use

Posted 25 October 2010 - 05:19 AM

hmmm... that's a smart solution. Going to try that.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: can't delete file, file in use

Posted 25 October 2010 - 06:23 AM

You could also kill the process when you know that the install has completed, just the user hasn't clicked "Finish"...
Was This Post Helpful? 0
  • +
  • -

#5 jellever  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 25-October 10

Re: can't delete file, file in use

Posted 25 October 2010 - 12:09 PM

Thanks for the replies! CCAuto Updater v1.3 will be released soon now :).

 Private Function remove_file_loop(ByVal file As String, ByVal times As Integer) As Boolean
        Try
            If IO.File.Exists(file) Then
                My.Computer.FileSystem.DeleteFile(file)
                Return True
            End If

        Catch ex As Exception
            If ex.GetHashCode = 63835064 Then
                If (times <= 20) Then
                    wait(500)
                    remove_file_loop(file, (times + 1))
                Else
                    MsgBox("A timeout occurred when updating. This is probably caused by a crashing installer. You must delete the update file manualy. The file is located at " + file)
                End If

            Else
                MsgBox("There is an unexpected exception when removing the update file: " + vbNewLine + ex.Message.ToString)
            End If

        End Try

    End Function

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1