Jump to content

Como fazer aplicação parar e esperar pelo download


redkiller
 Share

Recommended Posts

trata-se do seguinte é que a minha aplicação como tem de fazer um download de 50mb se necessitar esta fica a fazer o download mas segue logo para a extração e começa logo a dar erro pois nao fez o download por inteiro...

aqui ele entra e escolhe por exemplo o spr(50mb)

ElseIf update.sprver = update2.oldsprver AndAlso update.picver = update2.oldpicver AndAlso update.exever = update2.oldexever Then
                viewer.Text = "Updating dat"
                datDownload()
                Application.DoEvents()

            ElseIf update.datver = update2.olddatver AndAlso update.picver = update2.oldpicver AndAlso update.exever = update2.oldexever Then
                viewer.Text = "Updating spr"
                sprDownload()
                Application.DoEvents()

daqui segue para o sprDownload()

Sub sprDownload()
        'Downloads spr file
        Try

            Dim myClient1 As New WebClient()
            AddHandler myClient1.DownloadProgressChanged, AddressOf DownloadProgressCallback
            AddHandler myClient1.DownloadFileCompleted, AddressOf DownloadFileUnzip
            Dim url As New Uri("http://binoteam.no-ip.org/update/spr.zip")
            Dim file As String = My.Application.Info.DirectoryPath & "\Patch\spr.zip"

            myClient1.DownloadFileAsync(url, file)

            Application.DoEvents()

        Catch ex As Exception
            'Catches error and displays it.
            MessageBox.Show(ex.Message)
        End Try

        'Moves version.xml out of patch so old one gets replaced.
        Dim FileToCopy As String
        Dim NewCopy As String
        FileToCopy = (My.Application.Info.DirectoryPath & "\Patch\version.xml")
        NewCopy = (My.Application.Info.DirectoryPath & "\version.xml")
        'Copies ver.txt to different folder.
        If System.IO.File.Exists(FileToCopy) = True Then
            File.Delete("version.xml")
            System.IO.File.Copy(FileToCopy, NewCopy)
        End If
    End Sub

eu queria que ele parasse aqui em loop ou parasse até acabar o download

Como faço?

Obg

Link to comment
Share on other sites

vê estou a usar 1 sub para extrair todos mas n os refiro no fim dos subs que fazem o download checka

Imports System.Xml
Imports System.Xml.XPath
Imports System.IO
Imports System.Net
Imports System.ComponentModel
Imports ICSharpCode.SharpZipLib.Zip
Imports System.Threading


Public Class Main
    Private Sub frmMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        'Downloads version.xml

        Dim myClient As New WebClient()

        Dim url As New Uri("http://binoteam.no-ip.org/update/version.xml")
        Dim file As String = My.Application.Info.DirectoryPath & "\Patch\version.xml"

        AddHandler myClient.DownloadProgressChanged, AddressOf DownloadProgressCallback

        AddHandler myClient.DownloadFileCompleted, AddressOf DownloadFileCallBack2

        myClient.DownloadFileAsync(url, file)

        System.Threading.Thread.Sleep(5000)
    End Sub

    Private Sub DownloadProgressCallback(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs)
        pbrDownload.Value = e.ProgressPercentage
        lbxStatus.Text = (e.ProgressPercentage.ToString & " %")
    End Sub

    Private Sub DownloadFileCallBack2(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
        'CheckVersion()
        pbrDownload.Maximum = 100
        Try
            Dim doc = XDocument.Load(My.Application.Info.DirectoryPath & "\patch\version.xml")
            Dim doc2 = XDocument.Load(My.Application.Info.DirectoryPath & "\version.xml")

            Dim updates = From updater In doc.Descendants() _
            Select New With {.datver = updater.Element("version").Element("datver").Value, _
            .sprver = updater.Element("version").Element("sprver").Value, _
            .picver = updater.Element("version").Element("picver").Value, _
            .exever = updater.Element("version").Element("exever").Value}

            Dim updates2 = From updater In doc2.Descendants() _
            Select New With {.olddatver = updater.Element("version").Element("datver").Value, _
            .oldsprver = updater.Element("version").Element("sprver").Value, _
            .oldpicver = updater.Element("version").Element("picver").Value, _
            .oldexever = updater.Element("version").Element("exever").Value}

            Dim update = updates(0)
            Dim update2 = updates2(0)

            datver.Text = "New dat ver: " & update.datver
            sprver.Text = "New spr ver: " & update.sprver
            picver.Text = "New pic ver: " & update.picver
            exever.Text = "New exe ver: " & update.exever
            yourdatver.Text = "Old dat ver: " & update2.olddatver
            yoursprver.Text = "Old spr ver: " & update2.oldsprver
            yourpicver.Text = "Old pic ver: " & update2.oldpicver
            yourexever.Text = "Old exe ver: " & update2.oldexever


            If update.datver = update2.olddatver AndAlso update.sprver = update2.oldsprver AndAlso update.picver = update2.oldpicver AndAlso update.exever = update2.oldexever Then
                viewer.Text = "You're up-to-date"
            ElseIf update.sprver = update2.oldsprver AndAlso update.picver = update2.oldpicver AndAlso update.exever = update2.oldexever Then
                viewer.Text = "Updating dat"
                datDownload()
                Application.DoEvents()

            ElseIf update.datver = update2.olddatver AndAlso update.picver = update2.oldpicver AndAlso update.exever = update2.oldexever Then
                viewer.Text = "Updating spr"
                sprDownload()
                Application.DoEvents()

            ElseIf update.datver = update2.olddatver AndAlso update.sprver = update2.oldsprver AndAlso update.exever = update2.oldexever Then
                viewer.Text = "Updating pic"
                picDownload()
                Application.DoEvents()

            ElseIf update.datver = update2.olddatver AndAlso update.sprver = update2.oldsprver AndAlso update.picver = update2.oldpicver Then
                viewer.Text = "Updating exe"
                exeDownload()
                Application.DoEvents()

            ElseIf update.picver = update2.oldpicver AndAlso update.exever = update2.oldexever Then
                viewer.Text = "Updating dat"
                datDownload()
                Application.DoEvents()
                viewer.Text = "Updating spr"
                sprDownload()

            ElseIf update.sprver = update2.oldsprver AndAlso update.exever = update2.oldexever Then
                viewer.Text = "Updating dat"
                datDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating pic"
                picDownload()
                Thread.Sleep(5000)

            ElseIf update.sprver = update2.oldsprver AndAlso update.picver = update2.oldpicver Then
                viewer.Text = "Updating dat"
                datDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating exe"
                exeDownload()
                Thread.Sleep(5000)

            ElseIf update.datver = update2.olddatver AndAlso update.exever = update2.oldexever Then
                viewer.Text = "Updating spr"
                sprDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating pic"
                picDownload()
                Thread.Sleep(5000)

            ElseIf update.datver = update2.olddatver AndAlso update.picver = update2.oldpicver Then
                viewer.Text = "Updating spr"
                sprDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating exe"
                exeDownload()
                Thread.Sleep(5000)

            ElseIf update.datver = update2.olddatver AndAlso update.sprver = update2.oldsprver Then
                viewer.Text = "Updating spr"
                sprDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating pic"
                picDownload()
                Thread.Sleep(5000)

            ElseIf update.datver = update2.olddatver Then
                viewer.Text = "Updating spr"
                sprDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating pic"
                picDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating exe"
                exeDownload()
                Thread.Sleep(5000)

            ElseIf update.sprver = update2.oldsprver Then
                viewer.Text = "Updating dat"
                datDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating pic"
                picDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating exe"
                exeDownload()
                Thread.Sleep(5000)

            ElseIf update.picver = update2.oldpicver Then
                viewer.Text = "Updating dat"
                datDownload()
                Application.DoEvents()
                Thread.Sleep(5000)
                viewer.Text = "Updating spr"
                sprDownload()
                Application.DoEvents()
                Thread.Sleep(5000)
                viewer.Text = "Updating exe"
                exeDownload()
                Thread.Sleep(5000)

            ElseIf update.exever = update2.oldexever Then
                viewer.Text = "Updating dat"
                datDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating spr"
                sprDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating pic"
                picDownload()
                Thread.Sleep(5000)

            ElseIf update.datver <> update2.olddatver AndAlso update.sprver <> update2.oldsprver AndAlso update.picver <> update2.oldpicver AndAlso update.exever <> update2.oldexever Then
                viewer.Text = "Updating dat"
                datDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating spr"
                sprDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating pic"
                picDownload()
                Thread.Sleep(5000)
                Application.DoEvents()
                viewer.Text = "Updating exe"
                exeDownload()
                Thread.Sleep(5000)

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    Sub datDownload()
        'downloads dat file
        Try

            Dim myClient As New WebClient()
            AddHandler myClient.DownloadProgressChanged, AddressOf DownloadProgressCallback
            AddHandler myClient.DownloadFileCompleted, AddressOf DownloadFileUnzip
            Dim url As New Uri("http://binoteam.no-ip.org/update/dat.zip")
            Dim file As String = My.Application.Info.DirectoryPath & "\Patch\dat.zip"

            myClient.DownloadFileAsync(url, file)

            Application.DoEvents()

        Catch ex As Exception
            'Catches error and displays it.
            MessageBox.Show(ex.Message)
        End Try

        'Moves version.xml out of patch so old one gets replaced.
        Dim FileToCopy As String
        Dim NewCopy As String
        FileToCopy = (My.Application.Info.DirectoryPath & "\Patch\version.xml")
        NewCopy = (My.Application.Info.DirectoryPath & "\version.xml")
        'Copies ver.txt to different folder.
        If System.IO.File.Exists(FileToCopy) = True Then
            File.Delete("version.xml")
            System.IO.File.Copy(FileToCopy, NewCopy)
        End If
    End Sub

    Sub sprDownload()
        'Downloads spr file
        Try

            Dim myClient1 As New WebClient()
            AddHandler myClient1.DownloadProgressChanged, AddressOf DownloadProgressCallback
            AddHandler myClient1.DownloadFileCompleted, AddressOf DownloadFileUnzip
            Dim url As New Uri("http://binoteam.no-ip.org/update/spr.zip")
            Dim file As String = My.Application.Info.DirectoryPath & "\Patch\spr.zip"

            myClient1.DownloadFileAsync(url, file)

            Application.DoEvents()

        Catch ex As Exception
            'Catches error and displays it.
            MessageBox.Show(ex.Message)
        End Try

        'Moves version.xml out of patch so old one gets replaced.
        Dim FileToCopy As String
        Dim NewCopy As String
        FileToCopy = (My.Application.Info.DirectoryPath & "\Patch\version.xml")
        NewCopy = (My.Application.Info.DirectoryPath & "\version.xml")
        'Copies ver.txt to different folder.
        If System.IO.File.Exists(FileToCopy) = True Then
            File.Delete("version.xml")
            System.IO.File.Copy(FileToCopy, NewCopy)
        End If
    End Sub

    Sub picDownload()
        'Downloads pic file
        Try

            Dim myClient2 As New WebClient()
            AddHandler myClient2.DownloadProgressChanged, AddressOf DownloadProgressCallback
            AddHandler myClient2.DownloadFileCompleted, AddressOf DownloadFileUnzip
            Dim url As New Uri("http://binoteam.no-ip.org/update/pic.zip")
            Dim file As String = My.Application.Info.DirectoryPath & "\Patch\pic.zip"

            myClient2.DownloadFileAsync(url, file)

            Application.DoEvents()

        Catch ex As Exception
            'Catches error and displays it.
            MessageBox.Show(ex.Message)
        End Try

        'Moves version.xml out of patch so old one gets replaced.
        Dim FileToCopy As String
        Dim NewCopy As String
        FileToCopy = (My.Application.Info.DirectoryPath & "\Patch\version.xml")
        NewCopy = (My.Application.Info.DirectoryPath & "\version.xml")
        'Copies ver.txt to different folder.
        If System.IO.File.Exists(FileToCopy) = True Then
            File.Delete("version.xml")
            System.IO.File.Copy(FileToCopy, NewCopy)
        End If
    End Sub

    Sub exeDownload()
        'Downloads spr file
        Try

            Dim myClient3 As New WebClient()
            AddHandler myClient3.DownloadProgressChanged, AddressOf DownloadProgressCallback
            AddHandler myClient3.DownloadFileCompleted, AddressOf DownloadFileUnzip
            Dim url As New Uri("http://binoteam.no-ip.org/update/exe.zip")
            Dim file As String = My.Application.Info.DirectoryPath & "\Patch\exe.zip"

            myClient3.DownloadFileAsync(url, file)

            Application.DoEvents()

        Catch ex As Exception
            'Catches error and displays it.
            MessageBox.Show(ex.Message)
        End Try

        'Moves version.xml out of patch so old one gets replaced.
        Dim FileToCopy As String
        Dim NewCopy As String
        FileToCopy = (My.Application.Info.DirectoryPath & "\Patch\version.xml")
        NewCopy = (My.Application.Info.DirectoryPath & "\version.xml")
        'Copies ver.txt to different folder.
        If System.IO.File.Exists(FileToCopy) = True Then
            File.Delete("version.xml")
            System.IO.File.Copy(FileToCopy, NewCopy)
        End If
    End Sub

    Private Sub DownloadFileUnzip()
        pbrDownload.Maximum = 100

        Dim zipfile As New FastZip
        Try
            If File.Exists("Patch\dat.zip") Then
                Application.DoEvents()
                'This unzips dat.zip and updates dat file.
                zipfile.ExtractZip("Patch\dat.zip", ".", "")
                lbxStatus.Text = "Updated.»»(dat)!"
                'Deletes the zip folder to free up space.
                File.Delete("Patch\dat.zip")
                Thread.Sleep(200)

            ElseIf File.Exists("Patch\spr.zip") Then
                Application.DoEvents()
                'this unzips spr.zip and updates spr file.
                zipfile.ExtractZip("Patch\spr.zip", ".", "")
                lbxStatus.Text = "Updated.»»(spr)!"
                'deletes the zip folder to free up space.
                File.Delete("Patch\spr.zip")
                Thread.Sleep(200)

            ElseIf File.Exists("Patch\pic.zip") Then
                Application.DoEvents()
                'this unzips pic.zip and updates pic file.
                zipfile.ExtractZip("Patch\pic.zip", ".", "")
                lbxStatus.Text = "Updated.»»(pic)!"
                'deletes the zip folder to free up space.
                File.Delete("Patch\pic.zip")
                Thread.Sleep(200)

            ElseIf File.Exists("Patch\exe.zip") Then
                Application.DoEvents()
                'this unzips exe.zip and updates pic file.
                zipfile.ExtractZip("Patch\exe.zip", ".", "")
                lbxStatus.Text = "Updated.»»(exe)!"
                'deletes the zip folder to free up space.
                File.Delete("Patch\exe.zip")
                Thread.Sleep(200)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub DestroyUpdater()
        ' End the program.
        Me.Close()
        End
    End Sub
    Private Sub b_website_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles b_website.Click
        Shell("explorer.exe " & "http://binoteam.no-ip.org")
    End Sub
    Private Sub b_play_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles b_play.Click
        If pbrDownload.Value = 100 Then
            Shell("CB_Client.exe ")
            Me.Close()
            End
        End If
    End Sub
End Class
Link to comment
Share on other sites

Não podes, quando começares a fazer o download o ficheiro é logo criado. Imagina que estás a fazer o download de 2 ficheiros em simultâneo, o 2º acaba primeiro, o método é disparado, os dois ficheiros existem(conforme a verificação que estás a fazer), mas apenas o segundo está terminado e tu vais pegar no primeiro porque o ficheiro já existe, percebeste? Resumidamente, estoura.

Pedro Martins

Não respondo a duvidas por PM

Link to comment
Share on other sites

Sim, crias um método para cada download onde está apenas o seguinte código, mas para o ficheiro em questão.

                zipfile.ExtractZip("Patch\exe.zip", ".", "")
                lbxStatus.Text = "Updated.»»(exe)!"
                'deletes the zip folder to free up space.
                File.Delete("Patch\exe.zip")

Pedro Martins

Não respondo a duvidas por PM

Link to comment
Share on other sites

Tipo isto

            'Ficheiro 1
            Dim myClient As New WebClient()
            AddHandler myClient.DownloadProgressChanged, AddressOf DownloadProgressCallback
            AddHandler myClient.DownloadFileCompleted, AddressOf DownloadFileUnzip
            Dim url As New Uri("http://binoteam.no-ip.org/update/dat.zip")
            Dim file As String = My.Application.Info.DirectoryPath & "\Patch\dat.zip"
            myClient.DownloadFileAsync(url, file)
            'Ficheiro 2
            Dim myClient2 As New WebClient()
            AddHandler myClient2.DownloadProgressChanged, AddressOf DownloadProgressCallback
            AddHandler myClient2.DownloadFileCompleted, AddressOf DownloadFileUnzip2
            Dim url2 As New Uri("http://binoteam.no-ip.org/update/dat2.zip")
            Dim file2 As String = My.Application.Info.DirectoryPath & "\Patch\dat2.zip"
            myClient2.DownloadFileAsync(url2, file2)

Pedro Martins

Não respondo a duvidas por PM

Link to comment
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
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.