Stop zip file extaction

Gabrielo 1 Reputation point
2022-11-23T10:34:34.963+00:00

How do i stop/cancel extracting a zipfile in visual basic? theres no way to easily cancel and no matter what you do you cant stop or cancel from extracting until its actually finished
i even tried a thread but stopping it dosnet stop the zip actually extracting
Using archive As ZipArchive = ZipFile.OpenRead(zte)
'(code in here to deal with extracting zip)
End Using
thanks

Developer technologies | VB
{count} votes

1 answer

Sort by: Most helpful
  1. LesHay 7,146 Reputation points
    2022-11-24T18:00:10.187+00:00

    Hi
    Here is one way of doing it. This example is JUST to show a generalize approach as I do noy have your data etc.

    Setting up a simple BackGroundWorker to run your time consuming work. Allow the work to be interrupted and exit the BackGroundWorker using CancellationPending flag.
    The example just uses a Thread.Sleep(2 seconds) in a generalized loop counting from 1 to 10.

    Clicking the Start button starts the operation and the Stop button terminates the work.

    In effect, putting your code into a BackGroundWorker and place the check for CancellationPending in an appropriate place (such as directly after the extract code for one item)

    Example Form Design
    263975-111.png

    Example Code

    Option Strict On  
    Option Explicit On  
    Imports System.ComponentModel  
    Public Class Form1  
      Dim WithEvents bgw As New BackgroundWorker  
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        bgw.WorkerSupportsCancellation = True  
      End Sub  
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        'start operation  
        bgw.RunWorkerAsync()  
      End Sub  
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
        'stop operation  
        bgw.CancelAsync()  
      End Sub  
      Private Sub bgw_DoWork(sender As Object, e As DoWorkEventArgs) Handles bgw.DoWork  
        Invoke(Sub() Label1.Text = "Running")  
        Invoke(Sub() Label2.Text = Nothing)  
        For Each entry As Integer In {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}  
      
          'show loop counter in Label2  
          Invoke(Sub() Label2.Text = entry.ToString)  
      
          'emulate your operation with  
          ' a dummy 'pause'  
          Threading.Thread.Sleep(2000)  
      
          'check for exit  
          If bgw.CancellationPending Then  
            Exit For  
          End If  
        Next  
      End Sub  
      Private Sub bgw_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles bgw.RunWorkerCompleted  
        Label1.Text = "Stopped"  
      End Sub  
    End Class  
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.