Visualizzazione dei risultati da 1 a 10 su 10
  1. #1
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303

    [VB.NET]andare avanti quando si manifesta un'eccezzione

    come si procede per andare avanti quando si manifesta una eccezzione in questo codice
    codice:
            Try
                '  On Error Resume Next
                For i As Integer = 0 To Form1.ListBox2.Items.Count - 1
                    If remoteDevice.DeviceFileExists(Form1.ListBox2.Items(i)) = True Then
                        Dim remotedevice As New RAPI
                        Dim Attribs As RAPI.RAPIFileAttributes
                        Attribs = RAPI.RAPIFileAttributes.Normal
                        remotedevice.SetDeviceFileAttributes(Form1.ListBox2.Items(i), Attribs)
                        remotedevice.CopyFileFromDevice(Application.StartupPath & "\Root\" & Form1.ListBox2.Items(i), Form1.ListBox2.Items(i), True)
                        Label1.Text = Form1.ListBox2.Items(i)
                        Label2.Text = remotedevice.GetDeviceFileSize(Form1.ListBox2.Items(i))
                        Label3.Text = "Copiati: " & i + 1 & " di: " & Form1.ListBox2.Items.Count
                        PB1.Minimum = 0
                        PB1.Maximum = Form1.ListBox2.Items.Count
                        Application.DoEvents()
                        PB1.Value = i
                    End If
    
                Next
    
    
            Catch ex As Exception
                'MessageBox.Show(ex.Message)
                ListBox1.Items.Add(Label1.Text)
            End Try
            MessageBox.Show("Il Back-Up è stato eseguito!", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information)

  2. #2
    Non si può. Piuttosto sposta il try in modo che comprenda solo il codice che viene ripetuto nel for:
    codice:
            For i As Integer = 0 To Form1.ListBox2.Items.Count - 1
                If remoteDevice.DeviceFileExists(Form1.ListBox2.Items(i)) Then
                    Try
                        Dim remotedevice As New RAPI
                        Dim Attribs As RAPI.RAPIFileAttributes
                        Attribs = RAPI.RAPIFileAttributes.Normal
                        remotedevice.SetDeviceFileAttributes(Form1.ListBox2.Items(i), Attribs)
                        remotedevice.CopyFileFromDevice(Application.StartupPath & "\Root\" & Form1.ListBox2.Items(i), Form1.ListBox2.Items(i), True)
                        Label1.Text = Form1.ListBox2.Items(i)
                        Label2.Text = remotedevice.GetDeviceFileSize(Form1.ListBox2.Items(i))
                        Label3.Text = "Copiati: " & i + 1 & " di: " & Form1.ListBox2.Items.Count
                        PB1.Minimum = 0
                        PB1.Maximum = Form1.ListBox2.Items.Count
                        Application.DoEvents()
                        PB1.Value = i
                    Catch ex As Exception
                        ListBox1.Items.Add(Label1.Text)
                    End Try
                End If
    
            Next
            MessageBox.Show("Il Back-Up è stato eseguito!", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information)
    .
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    ciao,
    stò provando, toglimi una curiosità, ma in questo caso mi aggiunge tutte le eccezzioni, o solo la prima generata?

  4. #4
    Ovviamente tutte: quel codice si ripete ad ogni iterazione del ciclo.
    Amaro C++, il gusto pieno dell'undefined behavior.

  5. #5
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    ok,
    continuo a provare
    prima di aprire un'altro post, come posso ovviare quando l'applicazione è troppo impegnata ad elaborare, con logica conseguenza che il form mi avverte "Non risponde"?

  6. #6
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466
    Questa e' un'altra domanda rispetto a quella dell'eccezione (una sola z, mi raccomando ...) ...

    Rimedi con un DoEvents

  7. #7
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    ciao oregon,
    ma dove lo inserisco visto che nel codice eseguito è già presente!

  8. #8
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    Originariamente inviato da MItaly
    Non si può. Piuttosto sposta il try in modo che comprenda solo il codice che viene ripetuto nel for:
    codice:
            For i As Integer = 0 To Form1.ListBox2.Items.Count - 1
                If remoteDevice.DeviceFileExists(Form1.ListBox2.Items(i)) Then
                    Try
                        Dim remotedevice As New RAPI
                        Dim Attribs As RAPI.RAPIFileAttributes
                        Attribs = RAPI.RAPIFileAttributes.Normal
                        remotedevice.SetDeviceFileAttributes(Form1.ListBox2.Items(i), Attribs)
                        remotedevice.CopyFileFromDevice(Application.StartupPath & "\Root\" & Form1.ListBox2.Items(i), Form1.ListBox2.Items(i), True)
                        Label1.Text = Form1.ListBox2.Items(i)
                        Label2.Text = remotedevice.GetDeviceFileSize(Form1.ListBox2.Items(i))
                        Label3.Text = "Copiati: " & i + 1 & " di: " & Form1.ListBox2.Items.Count
                        PB1.Minimum = 0
                        PB1.Maximum = Form1.ListBox2.Items.Count
                        Application.DoEvents()
                        PB1.Value = i
                    Catch ex As Exception
                        ListBox1.Items.Add(Label1.Text)
                    End Try
                End If
    
            Next
            MessageBox.Show("Il Back-Up è stato eseguito!", "OK", MessageBoxButtons.OK, MessageBoxIcon.Information)
    .
    dunque il ciclo è appena finito, ma c'è qualcosa che non torna, di 962 file, ben 587 eccezzioni presentano lo stesso nome di file

  9. #9
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466
    Originariamente inviato da x69asterix
    ... eccezzioni
    Una z .... ....

    Prova ad inserire prima l'assegnazione alla label e usare quella ...

    codice:
       Attribs = RAPI.RAPIFileAttributes.Normal
       Label1.Text = Form1.ListBox2.Items(i)
       remotedevice.SetDeviceFileAttributes(Label1.Text, Attribs)
       remotedevice.CopyFileFromDevice(Application.StartupPath & "\Root\" & Label1.Text, Label1.Text, True)
       Label2.Text = remotedevice.GetDeviceFileSize(Label1.Text)

  10. #10
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    ok,
    adesso è corretto!!
    senza la ..Z..

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.