Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 在执行下一行代码之前,一行代码是否会完成?_Vb.net_Visual Studio 2010 - Fatal编程技术网

Vb.net 在执行下一行代码之前,一行代码是否会完成?

Vb.net 在执行下一行代码之前,一行代码是否会完成?,vb.net,visual-studio-2010,Vb.net,Visual Studio 2010,只要文件不存在,以下代码就会移动该文件。如果它这样做,它将不会移动文件 我的问题是关于文件.Move。msgbox何时显示?在文件完全移动后显示,还是在执行file.Move行后立即显示 根据文件大小,移动文件可能需要一段时间,因此我不希望在文件完全移动之前显示msgbox 有更好的方法吗 For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\Temp\", FileIO.SearchOption.

只要文件不存在,以下代码就会移动该文件。如果它这样做,它将不会移动文件

我的问题是关于
文件.Move
。msgbox何时显示?在文件完全移动后显示,还是在执行
file.Move
行后立即显示

根据文件大小,移动文件可能需要一段时间,因此我不希望在文件完全移动之前显示msgbox

有更好的方法吗

        For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\Temp\", FileIO.SearchOption.SearchAllSubDirectories, "*.zip")
            Dim foundFileInfo As New System.IO.FileInfo(foundFile)

            If My.Computer.FileSystem.FileExists("C:\Transfer\" & foundFileInfo.Name) Then
                Msgbox("File already exists and will not moved!")
                Exit Sub
            Else
                File.Move(foundFile, "C:\Transfer\" & foundFileInfo.Name)
                Msgbox("File has been moved!")
            End If
        Next

不幸的是,代码是一行接一行地执行的,因此只要文件被完全移动,就会弹出
Msgbox

如果要监视进度,.

相应地,
File.Move
调用是同步的,这意味着只有在移动文件后才会显示msgbox,而不管文件大小如何

为了完整性,如果您不想阻止UI,可以尝试以下方法:

' This must be placed outside your sub/function
Delegate Sub MoveDelegate(iSrc As String, iDest As String)

' This line and the following go inside your sub/function
Dim f As MoveDelegate = AddressOf File.Move

' Call File.Move asynchronously
f.BeginInvoke(
    foundFile, 
    "C:\Transfer\" & foundFile, 
    New AsyncCallback(Sub(r As IAsyncResult)
                          ' this code is executed when the move is complete
                          MsgBox("File has been moved!")
                      End Sub), Nothing)

或者,您可以浏览新的说明。

文件完全移动后,无论文件大小如何,都会显示消息框。

file.Move是一个同步操作,因此在移动完成之前,应用程序不会执行下一行代码(您的消息框)

正如您所指出的,如果文件很大(并且您正在跨驱动器移动),则在文件移动完成之前,messagebox不会显示。这可能会造成糟糕的用户体验,因为在此期间您的GUI似乎没有响应

我建议花点时间学习如何利用后台线程或异步/等待调用在后台执行操作

有一篇关于MSDN上异步IO的好文章:

最后,您还可以使用文件系统对象的方法,如果您只是担心保持UI响应,那么它可以为您弹出文件移动UI:

FileSystem.MoveFile(sourceFileName, destinationFileName, UIOption.AllDialogs)

除非一个方法是异步的,否则一行代码总是在继续执行下一行之前完成执行


注意,如果文件移动很慢,并且会阻塞程序是一件坏事,那么您可以使用例如a在后台线程中进行移动。

好的,我感觉它会完成,但希望在运行代码行之前确定。