Nicolò Favaretto
Nicolò Favaretto

Reputation: 49

Sub executed before another

I have the following sub

Public Sub Transfer()
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim Searching_Date As String
        Dim Name As String
        Dim Presente As Boolean
        Dim Foto_Presente As Boolean


        For i = 0 To CDIi - 1
            Searching_Date = Image_Date(Camera_Day_Images(i))
            Name = Replace(Camera_Day_Images(i), Camera & Path_in_Camera & "\", "")

            Presente = False
            j = 0
            While (Not Presente And j <= PCi)
                If (Path & "\" & Right_Date(Searching_Date)) = PC_Directory(j) Then
                    Presente = True
                Else
                    Presente = False
                End If
                j = j + 1
            End While

            If Presente = True Then
                Foto_Presente = False
                k = 0
                List_PC_Day_Images(Path & "\" & Right_Date(Searching_Date))
                While (Not Foto_Presente And k <= PDIi)
                    If (Path & "\" & Right_Date(Searching_Date) & "\" & Name) = PC_Day_Images(k) Then
                        Foto_Presente = True
                    Else
                        Foto_Presente = False
                    End If
                    k = k + 1
                End While
                If Foto_Presente = True Then

                Else
                    My.Computer.FileSystem.CopyFile(Camera & Path_in_Camera & "\" & Name, Path & "\" & Right_Date(Searching_Date) & "\" & Name)
                    PC_Day_Images(PDIi) = Path & "\" & Right_Date(Searching_Date) & "\" & Name
                    PDIi = PDIi + 1
                End If

            Else
                My.Computer.FileSystem.CreateDirectory(Path & "\" & Right_Date(Searching_Date))
                My.Computer.FileSystem.CopyFile(Camera & Path_in_Camera & "\" & Name, Path & "\" & Right_Date(Searching_Date) & "\" & Name)
            End If
        Next
        Principale.LFine.Text = "Tutte le tue foto sono state trasferite con successo"
        Principale.Button1.Enabled = False
    End Sub

It copies any photos from my device to my computer. So if I have a lot of photos It can take several time and I want to notify this. In fact I change the text in the label, than I call the Sub and finally rechange the label.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        LFine.Text = "attendere prego..."
        Transfer()
        LFine.Text = "Operazione completata con successo"
    End Sub

But the results are that Transfer () starts and just after he finished changing the label.

Why??? How can I fix this problem?? Thank you.

Upvotes: 0

Views: 53

Answers (3)

Josh Part
Josh Part

Reputation: 2164

That is because your process is a blocking process. The label is "updated" but the form is redrawn only after the whole process ends. Others have suggested to use Application.DoEvents() or LFine.Update, but the best way to do what you want is to make your process parallel.

You could use a BackgroundWorker for this:

Imports System.ComponentModel

Dim bgw As New BackgroundWorker

In the Load event of your form...

AddHandler bgw.DoWork, AddressOf bgw_DoWork
AddHandler bgw.RunWorkerCompleted, AddressOf bgw_RunWorkerCompleted

And then set your code like this...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    LFine.Text = "attendere prego..."
    bgw.RunWorkerAsync()
End Sub

Private Sub bgw_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgw.DoWork
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim Searching_Date As String
    Dim Name As String
    Dim Presente As Boolean
    Dim Foto_Presente As Boolean


    For i = 0 To CDIi - 1
        Searching_Date = Image_Date(Camera_Day_Images(i))
        Name = Replace(Camera_Day_Images(i), Camera & Path_in_Camera & "\", "")

        Presente = False
        j = 0
        While (Not Presente And j <= PCi)
            If (Path & "\" & Right_Date(Searching_Date)) = PC_Directory(j) Then
                Presente = True
            Else
                Presente = False
            End If
            j = j + 1
        End While

        If Presente = True Then
            Foto_Presente = False
            k = 0
            List_PC_Day_Images(Path & "\" & Right_Date(Searching_Date))
            While (Not Foto_Presente And k <= PDIi)
                If (Path & "\" & Right_Date(Searching_Date) & "\" & Name) = PC_Day_Images(k) Then
                    Foto_Presente = True
                Else
                    Foto_Presente = False
                End If
                k = k + 1
            End While
            If Foto_Presente = True Then

            Else
                My.Computer.FileSystem.CopyFile(Camera & Path_in_Camera & "\" & Name, Path & "\" & Right_Date(Searching_Date) & "\" & Name)
                PC_Day_Images(PDIi) = Path & "\" & Right_Date(Searching_Date) & "\" & Name
                PDIi = PDIi + 1
            End If

        Else
            My.Computer.FileSystem.CreateDirectory(Path & "\" & Right_Date(Searching_Date))
            My.Computer.FileSystem.CopyFile(Camera & Path_in_Camera & "\" & Name, Path & "\" & Right_Date(Searching_Date) & "\" & Name)
        End If
    Next
End Sub

Private Sub bgw_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted
    Principale.LFine.Text = "Tutte le tue foto sono state trasferite con successo"
    Principale.Button1.Enabled = False
End Sub

Just make sure you don't access any control of your form inside bgw_DoWork method.

Upvotes: 0

Tony Man
Tony Man

Reputation: 1

Wrap the call to tranfers() in an if statement

Change your Sub to a Function,

Public Function Transfer() As Boolean

...

then 

  If tranfers() = true then 

    LFine.Text = "Operazione completata con successo"
  End if

Upvotes: 0

rskar
rskar

Reputation: 4657

After LFine.Text = "attendere prego..." add this line:

LFine.Update()

See https://msdn.microsoft.com/en-us/library/vstudio/system.windows.forms.control.update(v=vs.100).aspx

Upvotes: 1

Related Questions