Stop aborting my thread!

J

Jerry Camel

The following code throws a "Thread was being aborted" exception at the Response.End line. Even if I catch the exception, the code that follows is never executed. (Because the thread was being aborted...?) What does this mean? Why is it happening? How do I fix it? I've tried everything I can think of. I've wrapped all the code in try blocks, but the thread exception is the only one being thrown. Please help... Thanks.

Jerry

sCommand = "SELECT * FROM FD_Files " & _
"WHERE (HashName = '" & e.CommandArgument() & "')"

Dim daFiles As New SqlDataAdapter(sCommand, sqlCnxn)
Dim dsFiles As New DataSet()
daFiles.Fill(dsFiles, "FD_Files")
Dim dr As DataRow = dsFiles.Tables("FD_Files").Rows(0)
Dim fStream As FileStream
Dim bytesToGo As Long
Dim bytesRead As Long
Dim byteBuffer(2048) As Byte

fStream = New FileStream("C:\DepotRoot\" & e.CommandArgument(), FileMode.Open, _
FileAccess.Read, FileShare.Read)
bytesToGo = fStream.Length
Response.BufferOutput = False
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "attachment; filename=""" & dr("FileName") & """")
Response.AppendHeader("Content-Length", fStream.Length)
Response.Flush()

While (bytesToGo > 0)
If (Response.IsClientConnected) Then
bytesRead = fStream.Read(byteBuffer, 0, 2048)
Response.OutputStream.Write(byteBuffer, 0, bytesRead)
Response.Flush()
bytesToGo -= bytesRead
Else
bytesToGo = -1
End If
End While

fStream.Close()
Response.End()
 
B

bruce barker

Response.End is implemented as a thread abort, so this is expected behavior.


-- bruce (sqlwork.com)


The following code throws a "Thread was being aborted" exception at the Response.End line. Even if I catch the exception, the code that follows is never executed. (Because the thread was being aborted...?) What does this mean? Why is it happening? How do I fix it? I've tried everything I can think of. I've wrapped all the code in try blocks, but the thread exception is the only one being thrown. Please help... Thanks.

Jerry

sCommand = "SELECT * FROM FD_Files " & _
"WHERE (HashName = '" & e.CommandArgument() & "')"

Dim daFiles As New SqlDataAdapter(sCommand, sqlCnxn)
Dim dsFiles As New DataSet()
daFiles.Fill(dsFiles, "FD_Files")
Dim dr As DataRow = dsFiles.Tables("FD_Files").Rows(0)
Dim fStream As FileStream
Dim bytesToGo As Long
Dim bytesRead As Long
Dim byteBuffer(2048) As Byte

fStream = New FileStream("C:\DepotRoot\" & e.CommandArgument(), FileMode.Open, _
FileAccess.Read, FileShare.Read)
bytesToGo = fStream.Length
Response.BufferOutput = False
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "attachment; filename=""" & dr("FileName") & """")
Response.AppendHeader("Content-Length", fStream.Length)
Response.Flush()

While (bytesToGo > 0)
If (Response.IsClientConnected) Then
bytesRead = fStream.Read(byteBuffer, 0, 2048)
Response.OutputStream.Write(byteBuffer, 0, bytesRead)
Response.Flush()
bytesToGo -= bytesRead
Else
bytesToGo = -1
End If
End While

fStream.Close()
Response.End()
 
J

Jerry Camel

Is that the proper way to terminate the file transfer, or should I be using something else? (Response.Close ?)
Or would a Response.Flush do the same thing?

I've worked around it by moving the response.end call to the end of the procedure, but I'm a bit new to web dev, so I want to learn the proper methods. Thanks.

Jerry
Response.End is implemented as a thread abort, so this is expected behavior.


-- bruce (sqlwork.com)


The following code throws a "Thread was being aborted" exception at the Response.End line. Even if I catch the exception, the code that follows is never executed. (Because the thread was being aborted...?) What does this mean? Why is it happening? How do I fix it? I've tried everything I can think of. I've wrapped all the code in try blocks, but the thread exception is the only one being thrown. Please help... Thanks.

Jerry

sCommand = "SELECT * FROM FD_Files " & _
"WHERE (HashName = '" & e.CommandArgument() & "')"

Dim daFiles As New SqlDataAdapter(sCommand, sqlCnxn)
Dim dsFiles As New DataSet()
daFiles.Fill(dsFiles, "FD_Files")
Dim dr As DataRow = dsFiles.Tables("FD_Files").Rows(0)
Dim fStream As FileStream
Dim bytesToGo As Long
Dim bytesRead As Long
Dim byteBuffer(2048) As Byte

fStream = New FileStream("C:\DepotRoot\" & e.CommandArgument(), FileMode.Open, _
FileAccess.Read, FileShare.Read)
bytesToGo = fStream.Length
Response.BufferOutput = False
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "attachment; filename=""" & dr("FileName") & """")
Response.AppendHeader("Content-Length", fStream.Length)
Response.Flush()

While (bytesToGo > 0)
If (Response.IsClientConnected) Then
bytesRead = fStream.Read(byteBuffer, 0, 2048)
Response.OutputStream.Write(byteBuffer, 0, bytesRead)
Response.Flush()
bytesToGo -= bytesRead
Else
bytesToGo = -1
End If
End While

fStream.Close()
Response.End()
 
K

Kevin Spencer

The Response ends all by itself. Why don't you just omit it?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.


Is that the proper way to terminate the file transfer, or should I be using
something else? (Response.Close ?)
Or would a Response.Flush do the same thing?

I've worked around it by moving the response.end call to the end of the
procedure, but I'm a bit new to web dev, so I want to learn the proper
methods. Thanks.

Jerry
Response.End is implemented as a thread abort, so this is expected
behavior.


-- bruce (sqlwork.com)


The following code throws a "Thread was being aborted" exception at the
Response.End line. Even if I catch the exception, the code that follows is
never executed. (Because the thread was being aborted...?) What does this
mean? Why is it happening? How do I fix it? I've tried everything I can
think of. I've wrapped all the code in try blocks, but the thread exception
is the only one being thrown. Please help... Thanks.

Jerry

sCommand = "SELECT * FROM FD_Files " & _
"WHERE (HashName = '" & e.CommandArgument() & "')"

Dim daFiles As New SqlDataAdapter(sCommand, sqlCnxn)
Dim dsFiles As New DataSet()
daFiles.Fill(dsFiles, "FD_Files")
Dim dr As DataRow = dsFiles.Tables("FD_Files").Rows(0)
Dim fStream As FileStream
Dim bytesToGo As Long
Dim bytesRead As Long
Dim byteBuffer(2048) As Byte

fStream = New FileStream("C:\DepotRoot\" & e.CommandArgument(),
FileMode.Open, _
FileAccess.Read, FileShare.Read)
bytesToGo = fStream.Length
Response.BufferOutput = False
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "attachment; filename=""" &
dr("FileName") & """")
Response.AppendHeader("Content-Length", fStream.Length)
Response.Flush()

While (bytesToGo > 0)
If (Response.IsClientConnected) Then
bytesRead = fStream.Read(byteBuffer, 0, 2048)
Response.OutputStream.Write(byteBuffer, 0, bytesRead)
Response.Flush()
bytesToGo -= bytesRead
Else
bytesToGo = -1
End If
End While

fStream.Close()
Response.End()
 
J

Jerry Camel

I had a problem before where data from a postback or some other page request
was being appended to the file transfer. It was apparent on text files
where the html ended up as part of the text file instead of showing in the
browser. I figured I needed to let the client know that the download was
complete so it would close the file on the client's end. There must be
something about this process that I'm not understanding properly... How
does the client side know when it has stopped receiving a download and is
now receiveing HTML to post? Thanks.

Jerry
 
K

Kevin Spencer

It's not a matter of what the client knows. You said yourself that your
first problem was caused by some output that your app was rendering AFTER
writing the correct data to the browser. As long as your app doesn't do
that, it certainly doesn't need to call Response.End, and it may in fact,
not be a good idea to do so.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
J

Jerry Camel

How would you update a web page after sending a file? There has to be some
indication that the file is sent, no? Apparently I'm missing something
here.
 
K

Kevin Spencer

How would you update a web page after sending a file? There has to be
some

You don't. The file is the Response. Every Response to a Request is a file.
A Request asks for a file. A Response sends one.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top