none
Managing files asynchronously RRS feed

  • Question

  • I have not been able to find the equivalent of My.Computer.FileSystem file handling functions for copying/moving/deleting files that work asynchronously.  If these do not exist, how can I handle a lot of files asynchronously so that my UI doesn't get bogged down waiting for the file processing to complete.
    Monday, April 8, 2019 5:26 PM

Answers

  • Hello,

    I have some code sample on various options that will do files or directories. I would suggest looking at a conventional method for doing BackGroundWorker then look at the async examples.

    https://code.msdn.microsoft.com/Copy-files-while-keeping-da349a4f?redir=0


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by SezMe Monday, April 8, 2019 10:59 PM
    Monday, April 8, 2019 6:01 PM
    Moderator

All replies

  • I'm curious, in past I called this creating rumble in the jungle. 

    Currently we have SSD drives. But those are AFAIK in Windows still working using a serial bus (for instance SATA). 

    Be aware Windows is a multiprogramming system, it is already build around a kind of time slices. 

    Probably if you want not get your computer bogged down for other processes, it is better to give your processes process priorities. 

    https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.priorityclass?view=netframework-4.7.2


    Success
    Cor

    Monday, April 8, 2019 5:49 PM
  • Hello,

    I have some code sample on various options that will do files or directories. I would suggest looking at a conventional method for doing BackGroundWorker then look at the async examples.

    https://code.msdn.microsoft.com/Copy-files-while-keeping-da349a4f?redir=0


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by SezMe Monday, April 8, 2019 10:59 PM
    Monday, April 8, 2019 6:01 PM
    Moderator
  • I have not been able to find the equivalent of My.Computer.FileSystem file handling functions for copying/moving/deleting files that work asynchronously.  If these do not exist, how can I handle a lot of files asynchronously so that my UI doesn't get bogged down waiting for the file processing to complete.

    Hi

    Here is some commented code to show an example of using a BackGroundWorker to keepUI responsive. This is a stand alone example and is completely pointless in that its only purpose is to show BGW use.

    In this example, I could have a ProgressBar being updated in the ReportProgress Sub, and of course, the required file operations would replace the dummy code I use for illustration. NOTE: there is no exception handling included.

    ' BLANK Form1 ' Copy/Replce existing Form1 code ' with this code. Option Explicit On Imports System.ComponentModel Public Class Form1 Dim WithEvents bgw As New BackgroundWorker Dim WithEvents Button1 As New Button Dim Label1 As New Label Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load With Label1 .Text = "Click Start" .Location = New Point(10, 40) End With With Button1 .Text = "Start" .AutoSize = True .Location = New Point(10, 10) End With With bgw .WorkerReportsProgress = True .WorkerSupportsCancellation = True End With Controls.AddRange({Button1, Label1}) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Select Case Button1.Text Case "Start" bgw.RunWorkerAsync() Button1.Text = "Stop" Case Else bgw.CancelAsync() Button1.Text = "Start" End Select End Sub Private Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork ' ============================ ' This block is ONLY for this ' example and would be ' replaced with your file ' handling code with appropriate ' care taken for Cancelling and ' Reporting.

            ' File Operations such as
            ' IO.File.Move(sourceFileName:=, destFileName:=)
            ' IO.File.Copy(sourceFileName:=, destFileName:=)
            ' IO.File.Delete(path:=)
            ' IO.File.Exists(path:=)
    Dim i As Integer = 1 Do If bgw.CancellationPending Then e.Cancel = True Exit Do End If bgw.ReportProgress(i) i += 1 ' add artificial delay to ' emulate work being done Threading.Thread.Sleep(100) Loop ' specifically using a never ' ending Loop to illustrate ' UI remaining responsive ' ============================ End Sub Private Sub bgw_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles bgw.ProgressChanged Label1.Text = "Completed " & e.ProgressPercentage.ToString & " Operations" End Sub Private Sub bgw_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted Label1.Text = "Click Start" End Sub End Class




    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, April 8, 2019 8:00 PM Added example IO methods
    Monday, April 8, 2019 6:40 PM
  • You can use IFileOperation (P/Invoke)

    Monday, April 8, 2019 6:45 PM
  • I was aware of the filestream possibility but that actually reads and writes the files which seems like a very inefficient way to just move a file.
    Monday, April 8, 2019 7:51 PM
  • I was aware of the filestream possibility but that actually reads and writes the files which seems like a very inefficient way to just move a file.
    If you were to look at the Microsoft source code (which I just did) for the .NET Framework FileStream class is used to copy files at the core.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, April 8, 2019 8:15 PM
    Moderator
  • That's interesting.  Seems counteri9ntuitive to me but, hey, I'm the student here.  I would appreciate a link.  I'll mark this as the answer and look again at the project you cited.
    Monday, April 8, 2019 10:59 PM