Partage
  • Partager sur Facebook
  • Partager sur Twitter

Mon Form se fige !

    15 décembre 2020 à 13:45:43

    Bonjour,

    J'utilise le code ci-dessous pour afficher dans une texBox le transfert de donnée généré par un Robocopy, mais lorsque j'utilise cette fonction pour différente transfert en simultané, mon form se fige jusqu'à la fin des transferts. Je sais que le problème provient de

    TxtSignatures_PnlMigration.AppendText(text & Environment.NewLine) 
    TxtSignatures_PnlMigration.ScrollToCaret()

    car lorsque je les retire, mon form ne se fige plus. Avez-vous une solution à se problème car j'aimerai afficher dans différents texbox le transfert des données du Robocopy de différents dossiers

    Voici mon code:

        Private Sub TransfertRobocopy_TextBox(ByVal PA_expediteur As String, ByVal PA_destinataire As String, ByVal ID_user As String, ByVal Dossier_transfert As String, ByVal lecteur_Expediteur As String, ByVal lecteur_Destinataire As String)
    
            Dim procInfo2 As New ProcessStartInfo
            Dim proc2 As New Process
            Dim Cible As String = """\\" & PA_expediteur & "\" & lecteur_Expediteur & "$\users\" & ID_user & "\" & Dossier_transfert & """ ""\\" & PA_destinataire & "\" & lecteur_Destinataire & "$\users\" & ID_user & "\" & Dossier_transfert & """ /E /V /TEE /w:5 /R:5" '/NFL /NDL /NJH sert à ne pas afficher les informations de telechargement
    
            TxtSignatures_PnlMigration.Clear()
    
            procInfo2.FileName = "C:\Windows\System32\Robocopy.exe"
            procInfo2.Arguments = Cible
            procInfo2.WindowStyle = ProcessWindowStyle.Hidden ' Sert à cacher la fenetre cmd robocopy lors du transfert
            procInfo2.UseShellExecute = False
            procInfo2.RedirectStandardError = True
            procInfo2.RedirectStandardOutput = True
            procInfo2.RedirectStandardInput = True
            procInfo2.CreateNoWindow = True
            procInfo2.StandardOutputEncoding = System.Text.Encoding.GetEncoding(850)
            procInfo2.StandardErrorEncoding = System.Text.Encoding.GetEncoding(850)
    
            proc2.StartInfo = procInfo2
            proc2.EnableRaisingEvents = True
            proc2.Start()
    
            AddHandler proc2.ErrorDataReceived, AddressOf Async_Data_Received_TextBox
            AddHandler proc2.OutputDataReceived, AddressOf Async_Data_Received_TextBox
            proc2.BeginOutputReadLine()
            proc2.BeginErrorReadLine()
        End Sub
    
        Private Delegate Sub InvokeWithString_TextBox(ByVal text As String)
        Private Sub Async_Data_Received_TextBox(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
            Invoke(New InvokeWithString_TextBox(AddressOf Sync_Output_TextBox), e.Data)
        End Sub
    
        Private Sub Sync_Output_TextBox(ByVal text As String)
            TxtSignatures_PnlMigration.AppendText(text & Environment.NewLine)
            TxtSignatures_PnlMigration.ScrollToCaret()
        End Sub

    Merci d'avance pour votre aide

    • Partager sur Facebook
    • Partager sur Twitter
      15 décembre 2020 à 14:31:41

      Bonjour,

      Comme le nom de la fonction l'indique, vous êtes en synchrone, donc normal que le form "se fige".

      Il faut le en asynchrone pour pouvoir continuer à utiliser le formulaire pendant le traitement

      • Partager sur Facebook
      • Partager sur Twitter
        15 décembre 2020 à 14:59:05

        • Partager sur Facebook
        • Partager sur Twitter
        Je recherche un CDI/CDD/mission freelance comme Architecte Logiciel/ Expert Technique sur technologies Microsoft.
          15 décembre 2020 à 14:59:52

          ce que j'avais fait à une époque pour mettre à jour une TextBox (de mémoire, ça fonctionnait):

          SetTextBoxText gère la mise jour du TextBox (donc à la place de dest.Text=text, tu peux y mettre le code de Sync_Output_TextBox

          et tu l'appelles dans ton Async_Datareceived_Textbox (cf dernière ligne)

              Private Delegate Sub SetTextBoxTextCallback(label As Object, text As String)
              Public Sub SetTextBoxText(obj As Object, text As String)
                  Dim dest As TextBox = TryCast(obj, TextBox)
          
                  If dest.InvokeRequired Then
                      Dim cb As New SetTextBoxTextCallback(AddressOf SetTextBoxText)
                      Me.Invoke(cb, New Object() {obj, text})
                  Else
                      dest.Text = text
                  End If
              End Sub
          [..]
          // et pour l'appeler et mettre à jour le TextBox de ton choix:
          SetTextBoxText(Me.TextBox1, text)



          • Partager sur Facebook
          • Partager sur Twitter
            16 décembre 2020 à 14:07:26

            même avoir tout essayé rien ne fonctionne..

                Private Sub Async_Data_Received_TextBox_Multiline_Signatures(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
            
                    SetTextBoxText(Invoke(New InvokeWithString_TextBox_Multiline_Signatures(AddressOf Sync_Output_TextBox_Multiline_Signatures), e.Data), Text)
                End Sub


            Avez-vous une autre solution ?

            • Partager sur Facebook
            • Partager sur Twitter
            Anonyme
              17 décembre 2020 à 9:32:51

              Hey, utilise un background Worker

              Cherche un peu si facile à comprendre et à trouver des exemples

              • Partager sur Facebook
              • Partager sur Twitter
                17 décembre 2020 à 11:42:58

                @Zonedetec A tu un exemple concret avec mon code ci-dessus à me proposer ?
                • Partager sur Facebook
                • Partager sur Twitter
                Anonyme
                  17 décembre 2020 à 13:16:31

                  Non je fais pas du c# avec des Dim et tout mais c'est quelque chose qui permet de ne pas bloquer l'UI pendant une boucle ou une action longue
                  • Partager sur Facebook
                  • Partager sur Twitter
                    17 décembre 2020 à 15:41:18

                    du c# avec des dim c'est du VB.Net ^^
                    • Partager sur Facebook
                    • Partager sur Twitter
                      21 décembre 2020 à 11:27:28

                      Toujours le même problème du form qui se fige ?

                      Savez-vous d’où vient ce problème ?

                      Voici le nouveau code :

                          Private Sub TransfertRobocopy_TextBox_Multiline_Videos(ByVal PA_expediteur As String, ByVal PA_destinataire As String, ByVal ID_user As String, ByVal Dossier_transfert As String, ByVal lecteur_Expediteur As String, ByVal lecteur_Destinataire As String)
                      
                              Dim procInfo2 As New ProcessStartInfo
                              Dim proc2 As New Process
                              Dim Cible As String = """\\" & PA_expediteur & "\" & lecteur_Expediteur & "$\users\" & ID_user & "\" & Dossier_transfert & """ ""\\" & PA_destinataire & "\" & lecteur_Destinataire & "$\users\" & ID_user & "\" & Dossier_transfert & """ /E /V /TEE /w:5 /R:5" '/NFL /NDL /NJH sert à ne pas afficher les informations de telechargement
                      
                              TxtVideos_PnlMigration.Clear()
                      
                              procInfo2.FileName = "C:\Windows\System32\Robocopy.exe"
                              procInfo2.Arguments = Cible
                              procInfo2.WindowStyle = ProcessWindowStyle.Hidden ' Sert à cacher la fenetre cmd robocopy lors du transfert
                              procInfo2.UseShellExecute = False
                              procInfo2.RedirectStandardError = True
                              procInfo2.RedirectStandardOutput = True
                              procInfo2.RedirectStandardInput = True
                              procInfo2.CreateNoWindow = True
                              procInfo2.StandardOutputEncoding = System.Text.Encoding.GetEncoding(850)
                              procInfo2.StandardErrorEncoding = System.Text.Encoding.GetEncoding(850)
                      
                              proc2.StartInfo = procInfo2
                              proc2.EnableRaisingEvents = True
                              proc2.Start()
                              GetID_Videos = proc2.Id
                      
                              AddHandler proc2.ErrorDataReceived, AddressOf Async_Data_Received_TextBox_Videos
                              AddHandler proc2.OutputDataReceived, AddressOf Async_Data_Received_TextBox_Videos
                              proc2.BeginOutputReadLine()
                              proc2.BeginErrorReadLine()
                      
                          End Sub
                      
                          Private Delegate Sub SetTextBoxTextCallback(label As Object, text As String)
                          Public Sub SetTextBoxText(obj As Object, text As String)
                      
                              Dim dest As TextBox = TryCast(obj, TextBox)
                      
                              If dest.InvokeRequired Then
                                  Dim cb As New SetTextBoxTextCallback(AddressOf SetTextBoxText)
                                  Me.Invoke(cb, New Object() {obj, text})
                              Else
                                  dest.AppendText(text & Environment.NewLine)
                                  dest.ScrollToCaret()
                              End If
                          End Sub
                          Private Sub Async_Data_Received_TextBox_Videos(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
                              SetTextBoxText(TxtVideos_PnlMigration, e.Data)
                          End Sub
                      



                      -
                      Edité par ffdu69 21 décembre 2020 à 11:37:48

                      • Partager sur Facebook
                      • Partager sur Twitter
                        23 décembre 2020 à 19:20:58

                        Pouvez-vous tracer, avec le débogueur, quel thread fait quoi et à quel moment ?
                        • Partager sur Facebook
                        • Partager sur Twitter
                        Je recherche un CDI/CDD/mission freelance comme Architecte Logiciel/ Expert Technique sur technologies Microsoft.

                        Mon Form se fige !

                        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
                        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
                        • Editeur
                        • Markdown