469,927 Members | 1,685 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Thread abort exception when running timer.

Hi,

Im running a timer to Unzip a zip file using a 'ICSharpCode.SharpZipLib.dll'.
It works well when I block the main thread indefinitely by 'thread.sleep(-1)'.
But gets an ThreadAbort Exception if i run the timer without thread blocking.
How can i avoid this error?Im not using thread.abort anywhere in my
code.My code to unzip file is taking only 6 sec to complete.
Below im showing the code.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Page_Load(ByVal sender As System.Object, ByVal e As   System.EventArgs) Handles MyBase.Load
  2.         fnInitiateZipTimer()
  3.        Thread.Sleep(-1)
  4. End Sub
  5.  
  6.  Private Sub objTimer_Elapsed(ByVal sender As Object, ByVal e As            System.Timers.ElapsedEventArgs) Handles objTimer.Elapsed
  7.        ExtractArchive_Global("F:\Projects\ZipExtract\Reviews")
  8.  End Sub
  9.  
  10. Sub ExtractArchive_Global(ByVal strPath As String)
  11.         Dim strException As String
  12.         Dim sourceDir As String
  13.         Dim destDir As String
  14.         Dim RenameDir As String
  15.         Dim StrFileName As String
  16.         Dim StrDir As String
  17.         Dim zipFilename As String
  18.         Dim ExtractDir As String
  19.         If Directory.Exists(strPath) = False Then
  20.             Directory.CreateDirectory(strPath)
  21.         End If
  22.         Try
  23.             sourceDir = strPath & "\pwr"
  24.             RenameDir = strPath & "\pwr-" & Format(Now(), "yyyy-MM-dd-hh-mm")
  25.             destDir = strPath & "\Archive\pwr-" & Format(Now(), "yyyy-MM-dd-hh-mm")
  26.             If Directory.Exists(sourceDir) Then
  27.                 System.IO.Directory.Move(sourceDir, RenameDir)
  28.                 If Directory.Exists(destDir) Then
  29.                     System.IO.Directory.Delete(destDir, True)
  30.                 End If
  31.                 System.IO.Directory.Move(RenameDir, destDir)
  32.             End If
  33.             zipFilename = strPath & "\pwr.zip"
  34.             ExtractDir = strPath
  35.             Dim Redo As Integer = 1
  36.             Dim MyZipInputStream As ZipInputStream
  37.             Dim MyFileStream As FileStream
  38.             MyZipInputStream = New ZipInputStream(New FileStream(zipFilename, _
  39.               FileMode.Open, FileAccess.Read))
  40.             Dim MyZipEntry As ZipEntry = MyZipInputStream.GetNextEntry
  41.             System.IO.Directory.CreateDirectory(ExtractDir)
  42.             While Not MyZipEntry Is Nothing
  43.                 If (MyZipEntry.IsDirectory) Then
  44.                     Try
  45.                         System.IO.Directory.CreateDirectory(ExtractDir & "\" & MyZipEntry.Name)
  46.                     Catch ex As Exception
  47.                     End Try
  48.                 Else
  49.                     Dim _strPath As String
  50.                     Try
  51.                         If Not System.IO.Directory.Exists(ExtractDir & "\" & _
  52.                         System.IO.Path.GetDirectoryName(MyZipEntry.Name)) Then
  53.  
  54.                             _strPath = System.IO.Path.GetDirectoryName(MyZipEntry.Name)
  55.                             System.IO.Directory.CreateDirectory(ExtractDir & "\" & _strPath)
  56.                         End If
  57.                     Catch ex As ThreadAbortException
  58.                         Thread.ResetAbort()
  59.                     End Try
  60.                     Try
  61.                         MyFileStream = New System.IO.FileStream(ExtractDir & "\" & _
  62.                      MyZipEntry.Name, FileMode.OpenOrCreate, FileAccess.Write)
  63.                         Dim count As Integer
  64.                         Dim buffer(50000) As Byte
  65.                         count = MyZipInputStream.Read(buffer, 0, 50000)
  66.                         While count > 0
  67.                             MyFileStream.Write(buffer, 0, count)
  68.                             count = MyZipInputStream.Read(buffer, 0, 50000)
  69.                         End While
  70.                         buffer = Nothing
  71.                         MyFileStream.Close()
  72.                     Catch ex As Exception
  73.                     End Try
  74.                 End If
  75.                 MyZipEntry = MyZipInputStream.GetNextEntry
  76.             End While
  77.             If Not (MyZipInputStream Is Nothing) Then
  78.                 MyZipInputStream.Close()
  79.             End If
  80.             If Not (MyFileStream Is Nothing) Then
  81.                 MyFileStream.Close()
  82.             End If
  83.         Catch ex As ThreadAbortException
  84.             Thread.ResetAbort()
  85.         End Try
  86.    End Sub
Aug 2 '07 #1
0 1155

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Dan S | last post: by
7 posts views Thread by Morris | last post: by
20 posts views Thread by Doug Thews | last post: by
7 posts views Thread by Doug Thews | last post: by
18 posts views Thread by Urs Vogel | last post: by
reply views Thread by =?Utf-8?B?aGVyYmVydA==?= | last post: by
5 posts views Thread by John A. Bailo | last post: by
20 posts views Thread by =?ISO-8859-1?Q?Gerhard_H=E4ring?= | last post: by
3 posts views Thread by yeye.yang | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.