[VB.NET] Comment créer un chromètre

Fermé
BobyCode Messages postés 9 Date d'inscription jeudi 30 octobre 2014 Statut Membre Dernière intervention 4 novembre 2014 - Modifié par Whismeril le 1/11/2014 à 00:11
BobyCode Messages postés 9 Date d'inscription jeudi 30 octobre 2014 Statut Membre Dernière intervention 4 novembre 2014 - 31 oct. 2014 à 16:04
Bonjour,

Alors voilà, je voudrais créer un chronomètre et un temps restant (comme quand vous télécharger quelques chose sur votre navigateur). J'utilise Timer pour cela mais j'ai un problème quand je lance le Timer dans ma fonction, il ne se lance pas alors que dans le Form_load ça marche.

Voici mon code :
    Private Sub ExcelExtract(ByVal chemin As String)
        LabelX1.Text = "Initialisation du fichier..."
        LabelX2.Visible = True
        Cursor = Cursors.WaitCursor
        ProgressBarX1.Value = 0

        'Excel

        'Définition des variables
        Dim xlApp As Application
        Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xl1 As Microsoft.Office.Interop.Excel.Worksheet

        ' nettoyage des tables temporaires avant importation des données
        ExecuteSql("DELETE FROM poste")
        ExecuteSql("ALTER TABLE poste AUTO_INCREMENT=1")

        'Code
        xlApp = New Microsoft.Office.Interop.Excel.Application

        xlWorkBook = xlApp.Workbooks.Open(chemin)
        xl1 = xlWorkBook.Worksheets(1)

        'Définition des mois
        moisDebut = ComboBoxEx1.Text
        moisFin = ComboBoxEx3.Items(ComboBoxEx3.SelectedIndex + 1).text

        'Initialisation, définir les colonnes et les lignes
        Dim firstLigne As Integer = 1
        Dim firstColumn As Integer
        Dim lastLigne As Integer = xl1.Cells.SpecialCells(XlCellType.xlCellTypeLastCell).Row
        Dim lastColumnAll As Integer = xl1.Cells.SpecialCells(XlCellType.xlCellTypeLastCell).Column
        Dim lastColumn As Integer

        Dim columnPostes As Integer
        Dim columnMachines As Integer = 1
        Dim columnEquipe As Integer
        Dim ligneMois As Integer
        Dim ligneJour As Integer

        Dim nomMachine As String = ""

        'Définiton des variables colonnes et lignes
        Dim quitter As Integer = 0

        For j As Integer = 1 To lastColumnAll
            For i As Integer = 1 To lastLigne
                If xl1.Cells(i, j).text = "JANVIER" Then
                    Dim quitterMois As Integer = 0
                    For l As Integer = j To lastColumnAll
                        If xl1.Cells(i, l).text = moisDebut Then
                            ligneMois = i
                            firstColumn = l
                            ligneJour = ligneMois + 3
                            quitter += 1
                            quitterMois += 1
                        ElseIf xl1.Cells(i, l).text = moisFin Then
                            lastColumn = l - 1
                            quitter += 1
                            quitterMois += 1
                        End If
                        If quitterMois = 2 Then Exit For
                    Next
                ElseIf xl1.Cells(i, j).text = "Postes" Then
                    columnPostes = j
                    quitter += 1
                ElseIf xl1.Cells(i, j).text = "Equipe" Then
                    If columnEquipe <> j Then
                        columnEquipe = j
                        quitter += 1
                    End If
                End If
                If quitter = 4 Then Exit For
            Next
            If quitter = 4 Then Exit For
        Next

        LabelX1.Text = "Extraction des données... (Cette opération peut prendre quelques minutes)"

        For i As Integer = firstLigne To lastLigne 'Lignes du tableau
            ProgressBarX1.Value = i * 100 / (lastLigne - firstLigne)
            LabelX2.Text = "" & Math.Round((i * 100 / (lastLigne - firstLigne)), 0) & " %"
            If xl1.Cells(i, columnMachines).value <> "" Then
                nomMachine = xl1.Cells(i, columnMachines).value
                For j As Integer = 0 To 2
                    For col As Integer = firstColumn To lastColumn 'Colonnes du tableau
                        If xl1.Cells(i + j, col).Interior.colorindex = 48 And xl1.Cells(i + j, col).value Is Nothing Then
                            'Trouver le mois
                            Dim words As String = xl1.Cells(ligneMois, col).MergeArea.Address.ToString()
                            Dim split As String() = words.Split(New [Char]() {":"c, CChar(vbTab)})
                            ExecuteSql("INSERT INTO poste(nomPoste, nomMachine,Equipe,date)" & _
                                       "values('" & xl1.Cells(i, columnPostes).value & "'," & _
                                       "'" & nomMachine & "'," & _
                                       "'" & xl1.Cells(i + j, columnEquipe).value & "'," & _
                                       "'" & xl1.Cells(ligneJour, col).value.ToString() & " " & xl1.Range("" & split(0) & "").Value & " 2014')")
                        End If
                    Next
                Next
            ElseIf xl1.Cells(i, columnPostes).value <> "" Then
                For j As Integer = 0 To 2
                    For col As Integer = firstColumn To lastColumn 'Colonnes du tableau
                        If xl1.Cells(i + j, col).Interior.colorindex = 48 And xl1.Cells(i + j, col).value Is Nothing Then
                            Dim words As String = xl1.Cells(ligneMois, col).MergeArea.Address.ToString()
                            Dim split As String() = words.Split(New [Char]() {":"c, CChar(vbTab)})
                            ExecuteSql("INSERT INTO poste(nomPoste, nomMachine,Equipe,date)" & _
                                       "values('" & xl1.Cells(i, columnPostes).value & "'," & _
                                       "'" & nomMachine & "'," & _
                                       "'" & xl1.Cells(i + j, columnEquipe).value & "'," & _
                                       "'" & xl1.Cells(ligneJour, col).value.ToString() & " " & xl1.Range("" & split(0) & "").Value & " 2014')")
                        End If
                    Next
                Next
            Else
            End If
        Next

        'Active app could change before this runs

        xlWorkBook.Close(False)
        xlWorkBook = Nothing

        xlApp.Quit()
        xlApp = Nothing

        System.GC.Collect()
        System.GC.WaitForPendingFinalizers()
        System.GC.Collect()
        System.GC.WaitForPendingFinalizers()

        LabelX1.Text = "Success!"
        ProgressBarX1.Value = 100
        Cursor = Cursors.Default
        ButtonX1.Visible = True
        LabelX3.Visible = True
        ProgressBarX2.Visible = True
    End Sub



Merci :)
A voir également:

3 réponses

cs_Le Pivert Messages postés 7903 Date d'inscription jeudi 13 septembre 2007 Statut Contributeur Dernière intervention 11 mars 2024 728
31 oct. 2014 à 08:54
Bonjour,

C'est normal que le timer ne fonctionne pas pendant la durée de la tâche demandée. Il faut utiliser un thread qui agit en arrière plan. Voir ceci pour la mise en oeuvre d'un thread:

https://plasserre.developpez.com/cours/vb-net/?page=windows-forms5#LX-R
1
BobyCode Messages postés 9 Date d'inscription jeudi 30 octobre 2014 Statut Membre Dernière intervention 4 novembre 2014
31 oct. 2014 à 16:04
Oui du coup on m'a conseillé d'utiliser BackgroudWorker :)
Merci !
0
ant.r72 Messages postés 1807 Date d'inscription samedi 28 juin 2014 Statut Contributeur Dernière intervention 12 novembre 2020 526
30 oct. 2014 à 16:19
Bonjour,
Si tu utilise Microsoft Visual Basic ou Visual Studio, le timer est généralement désactivé par défaut.

Pour l'activer, règle la propriété Enabled sur True
0
BobyCode Messages postés 9 Date d'inscription jeudi 30 octobre 2014 Statut Membre Dernière intervention 4 novembre 2014
30 oct. 2014 à 16:24
Oui mais même quand je l'active, mon timer commence à la fin de la fonction ou ne fonctionne pas du tout
0
BobyCode Messages postés 9 Date d'inscription jeudi 30 octobre 2014 Statut Membre Dernière intervention 4 novembre 2014
30 oct. 2014 à 16:28
Je voudrais que un chronomètre s'affiche sur mon application (via un label) du temps que mets la fonction à se faire, un peu comme quand on télécharge un truc sur un navigateur par exemple.
0
ant.r72 Messages postés 1807 Date d'inscription samedi 28 juin 2014 Statut Contributeur Dernière intervention 12 novembre 2020 526
30 oct. 2014 à 16:31
Je vient de comprendre. Je sais pas du tout pour faire ça. Désolé.
0
BobyCode Messages postés 9 Date d'inscription jeudi 30 octobre 2014 Statut Membre Dernière intervention 4 novembre 2014
30 oct. 2014 à 16:35
Merci quand même ;)
0
ant.r72 Messages postés 1807 Date d'inscription samedi 28 juin 2014 Statut Contributeur Dernière intervention 12 novembre 2020 526
30 oct. 2014 à 16:30
Tu rentre bien la fonction directement dans le timer (en double - cliquant dessus) ?
Par exemple :

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim mavariable As Integer
mavariable = mavariable + 1
Label1.text = mavariable
End Sub


Je m'y connais pas trop en Vb.net
0
BobyCode Messages postés 9 Date d'inscription jeudi 30 octobre 2014 Statut Membre Dernière intervention 4 novembre 2014
30 oct. 2014 à 16:32
En gros je fais ça :

Dim mavariable As Integer
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
mavariable = mavariable + 1
Label1.text = mavariable
End Sub


et ensuite je mets :
Timer1.Start()

dans ma fonction

Mais ça ne marche pas
0