none
How to run ZipFile.CreateFromDirectory(Source, ZipFull) asynchronously RRS feed

  • Question

  • Know how Windows reports the progress of file copying operations, etc.? I want to do that when creating a Zip file with the CreateFromDirectory routine. But I can't seem to do that, I think because that routine doesn't run in async. In other words, I think once the routine starts, the program can't do anything (like report the zipping process in a progress bar) until it finishes. I need to be able to get stuff done (show progress on a progress bar) while the routine is operating.

    I've seen some examples of how to do this, but they are too complicated for me. Can anyone supply a simple "black box" routine to do this? Thanks!


    Robert Homes

    Monday, September 3, 2018 3:40 PM

All replies

  • Know how Windows reports the progress of file copying operations, etc.? I want to do that when creating a Zip file with the CreateFromDirectory routine. But I can't seem to do that, I think because that routine doesn't run in async. In other words, I think once the routine starts, the program can't do anything (like report the zipping process in a progress bar) until it finishes. I need to be able to get stuff done (show progress on a progress bar) while the routine is operating.

    I've seen some examples of how to do this, but they are too complicated for me. Can anyone supply a simple "black box" routine to do this? Thanks!

    I just found another thread here that mentions: "are you sure that threading (via a BackgroundWorker) wouldn't suffice?" (from "Frank"). That sounds like it might be a lot simpler, but I know nothing about threads in a VB program. Can anyone give me a quick rundown on that approach?


    Robert Homes


    Monday, September 3, 2018 3:43 PM
  • Hi

    I posted a simple example of BacjGroundWorker HERE in this forum. Give it a check out and see if it helps. NOTE: there are other events that can be handled - ProgressChanged (useful for updating a ProgressBar) and RunWorkerCompleted which is called after  BGW has done it's work.

    Here is the same code but added those empty handlers.

    ' Form1 with several
    ' controls in a Panel1
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboBox1.SelectedIndex = 0
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        BackgroundWorker1.RunWorkerAsync()
      End Sub
      Sub AddLBitem(s As String)
        ' to prevent cross thread 
        ' exceptions
        If InvokeRequired Then
          Invoke(Sub() ListBox1.Items.Add(s))
        Else
          ListBox1.Items.Add(s)
        End If
      End Sub
      Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        ' This is main worker routine
        For Each c As Control In Panel1.Controls
          AddLBitem(c.Name)
        Next
      End Sub
      Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ' called from worker thread and
        ' can Update main thread controls
        ' etc (ProgressBar, Labels etc)
      End Sub
      Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        ' completed work
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Monday, September 3, 2018 3:57 PM
  • As far as I can see in the sources of ZipFile there are nothing to support notification of the progress.

    The best what I can offer - take the source from Microsoft and modify it for notification after each file. The rest you will be able to do with standard BackgroundWorker.

    Best regards,

    Andrey

    Monday, September 3, 2018 4:19 PM