Sub在另一个之前执行

时间:2015-06-19 19:14:52

标签: vb.net

我有以下子

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

它会将我设备中的所有照片复制到计算机上。所以如果我有很多照片可能需要几次,我想通知这个。实际上我更改了标签中的文本,而不是调用Sub并最终重新更改标签。

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

但结果是Transfer()在他完成标签更改后开始。

为什么???我该如何解决这个问题? 谢谢。

3 个答案:

答案 0 :(得分:1)

LFine.Text = "attendere prego..."添加此行后:

LFine.Update()

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

答案 1 :(得分:0)

tranfers()声明

中打电话给if

将您的Sub更改为Function

Public Function Transfer() As Boolean

...

then 

  If tranfers() = true then 

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

答案 2 :(得分:0)

这是因为您的流程是阻止流程。标签已更新&#34;但只有在整个过程结束后才会重新绘制表单。其他人建议使用Application.DoEvents()LFine.Update,但最好的方法就是让你的流程平行。

您可以使用BackgroundWorker

Imports System.ComponentModel

Dim bgw As New BackgroundWorker

在您表单的Load事件中......

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

然后像这样设置你的代码......

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

请确保您无法在bgw_DoWork方法中访问对表单的任何控件。