Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3212474
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T14:50:31+00:00 2026-05-17T14:50:31+00:00

I’m using vb.net 2005, I’ve got the following code running a thread to download

  • 0

I’m using vb.net 2005, I’ve got the following code running a thread to download a file. However, the process fails sometimes when trying to read the local copy of the file. I think I may need to unlock the local file somehow but I’m not sure how to do this. Can someone take a look and advise me ?

Dim BP1Ended As Boolean = False
Private Sub BackgroundProcess1()
    BP1Ended = False
    mPadFileStatus = DownloadFile(mstrPadUrl, mLocalFile)
    BP1Ended = True
End Sub

'---'
Dim t As System.Threading.Thread

t = New System.Threading.Thread(AddressOf BackgroundProcess1)
t.Start()

Dim ProcessStartTime As Date = Now()

Do While ProcessStartTime.AddMinutes(1) >= Date.Now 

Application.DoEvents()
If BP1Ended = True Then
    Exit Do
End If
Loop
t.Abort()

PadFileStatus = mPadFileStatus

If BP1Ended = False Then
    Application.DoEvents()
    AddConsoleMsg("Downloading file.... Aborted", True)
End If

'---'
Public Function DownloadFile(ByVal pstrRequestedFile As String, ByVal pstrDestinationFile As String, Optional ByVal TimeOut As Integer = 120) As DownloadStatus
Dim input As IO.Stream
Dim Req As System.Net.HttpWebRequest = Nothing
Dim Response As System.Net.HttpWebResponse

Try
    Req = System.Net.HttpWebRequest.Create(pstrRequestedFile)
Catch ex As Exception                       
    Return DownloadStatus.UnknownError
End Try

Req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"

Req.Timeout = TimeOut * 1000 '120 * 1000 '1 second = 1 000 milliseconds
Try
    Response = Req.GetResponse
    input = Response.GetResponseStream

    Dim streamreader As New StreamReader(input, System.Text.Encoding.GetEncoding("windows-1252")) 'System.Text.Encoding.UTF8)'
    Dim s_response As String = streamreader.ReadToEnd()
    streamreader.Close()

    Dim filestream As New FileStream(pstrDestinationFile, FileMode.Create)
    Dim streamwriter As New StreamWriter(filestream, System.Text.Encoding.GetEncoding("windows-1252")) ' System.Text.Encoding.UTF8)'
    streamwriter.Write(s_response)
    streamwriter.Flush()
    streamwriter.Close()

    Dim length As Long = 1000000 * 100
    Dim pos As Long = 0
    If Response.ContentLength > 0 Then
    length = Response.ContentLength
    End If

    If length > 0 Then
    Return DownloadStatus.OK
    End If

    input.Close()

Catch ew As System.Net.WebException

    If ew.Status = WebExceptionStatus.NameResolutionFailure Or ew.Status = WebExceptionStatus.ProtocolError Then
    Return DownloadStatus.FileNotFound
    ElseIf ew.Status <> WebExceptionStatus.Success Then
    Return DownloadStatus.UnknownError
    End If
    'Dim errorRespone As HttpWebResponse = CType(ew.Response, HttpWebResponse)
    'If errorRespone.StatusCode = HttpStatusCode.NotFound Then '404
    '    Return DownloadStatus.FileNotFound
    'Else
    '    Return DownloadStatus.UnknownError
    'End If'
Catch ex As Exception                                          'Don't know' 

    Return DownloadStatus.UnknownError
End Try

End Function

'---'
Dim OpenFile As FileStream
'OpenFile = New FileStream(pstrPadFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
'FAILS HERE
OpenFile = New FileStream(pstrPadFile, FileMode.Open, FileAccess.Read, FileShare.Read)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T14:50:32+00:00Added an answer on May 17, 2026 at 2:50 pm

    You don’t close the file stream in case of exception which might lead to the lock. Make sure you always dispose disposable resources by wrapping them in Using statements:

    Using filestream As FileStream = New FileStream(pstrDestinationFile, FileMode.Create)
        Using streamwriter As StreamWriter = New StreamWriter(filestream, Encoding.GetEncoding("windows-1252"))
            streamwriter.Write(s_response)
        End Using
    End Using
    

    Same stands true for the response and response streams. They should be properly disposed.

    Also you may consider using the WebClient class which has methods like DownloadFile and DownloadData which could make your life much easier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.