Results 1 to 8 of 8

Thread: Data Copy Operation and Process excecution

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    14

    Data Copy Operation and Process excecution

    Hi,

    I would like to copy a folder (with all it's contents - files /folders /hidden files folders /empty directories etc.)
    And need to use this newly copied data to start a process, ... but if the copying operation is not finished already, the next process will give error.

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    My.Computer.FileSystem.CopyDirectory("C:\CopyFileDemo", "D:\CopyFileDemo")
    Process.Start("D:\CopyFileDemo\MyData\MyApp.exe")
    End Sub

    In other words, I am looking for a way to copy directory first
    My.Computer.FileSystem.CopyDirectory("C:\CopyFileDemo", "D:\CopyFileDemo")

    and once this task is finished, proceed with the next task:
    Process.Start("D:\CopyFileDemo\MyData\MyApp.exe")

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Data Copy Operation and Process excecution

    This is just a guess... CopyDirectory works synchronously, so it should be all finished by your process line. Try putting an application.DoEvents between the two lines.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    14

    Re: Data Copy Operation and Process excecution

    Hi Paul and thanks for reply.
    Do you have an example of this concept?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Data Copy Operation and Process excecution

    When you post code in this forum, select your code block and click the # (code) button in the post editor window...

    Code:
    My.Computer.FileSystem.CopyDirectory("C:\CopyFileDemo", "D:\CopyFileDemo")
    Application.DoEvents
    Process.Start("D:\CopyFileDemo\MyData\MyApp.exe")

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2020
    Posts
    14

    Re: Data Copy Operation and Process excecution

    Quote Originally Posted by .paul. View Post
    When you post code in this forum, select your code block and click the # (code) button in the post editor window...

    Code:
    My.Computer.FileSystem.CopyDirectory("C:\CopyFileDemo", "D:\CopyFileDemo")
    Application.DoEvents
    Process.Start("D:\CopyFileDemo\MyData\MyApp.exe")
    And with this approach (Application.DoEvents), the command after this line will process only if the previous is finished, correct?

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Data Copy Operation and Process excecution

    The previous command is synchronous, meaning it should finish before continuing execution. As this is not happening, the DoEvents might help. It’s not something that is lightly recommended, but in these circumstances it might work...

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Data Copy Operation and Process excecution

    From the original question, I'm not clear whether there actually IS a problem. I would expect CopyDirectory to be synchronous, and .paul. says that it is, in which case, everything should copy over before the Process.Start line is reached. You don't say that this doesn't happen, only that it would be bad if it did happen. Is it not happening? And are you sure that's the right problem?

    I'm not sure that DoEvents will help, in this case. DoEvents will pause to process any pending events, but it seems like what would happen in CopyDirectory might not be an event that DoEvents would have any impact on (such as file copying resulting in a modal messagebox). So, you could put it in there to give it a try, I'm just thinking it won't solve whatever problem you are having. If it does solve it, you'd probably want to study further, because you'd want to know WHY DoEvents fixed the problem, or else it may not have really been solved. If DoEvents doesn't solve it, then I think you should explain what you are seeing and how you decided that the files weren't all being copied over.
    My usual boring signature: Nothing

  8. #8
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    579

    Re: Data Copy Operation and Process excecution

    From current .NET runtime source (FileSystem.vb source from .NET Runtime):

    VB.NET Code:
    1. ' NOTE: Decision to create target directory is different for Shell and Framework call.
    2.  
    3. If showUI <> UIOptionInternal.NoUI AndAlso Environment.UserInteractive Then
    4.     ShellCopyOrMove(operation, FileOrDirectory.Directory, SourceDirectoryFullPath, TargetDirectoryFullPath, showUI, onUserCancel)
    5. Else
    6.     ' Otherwise, copy the directory using System.IO.
    7.     FxCopyOrMoveDirectory(operation, SourceDirectoryFullPath, TargetDirectoryFullPath, overwrite)
    8. End If

    According to use of showUI parameter, the copying is performed in two completely different ways. So it depends on method overloads and some other settings. ShellCopyOrMove uses SHFileOperation to perform the copy when UI is required and it is not clear how it is done (requires more analysis of involved Windows APIs).

    FxCopyOrMoveDirectory method is just pure .NET code that can be used if the OP wants such implementation.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width