Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Accessing HTMLInputFile across thread boundry?

Status
Not open for further replies.

ThatRickGuy

Programmer
Oct 12, 2001
3,841
US
Hey Guys,
I'm working on a site that the user will upload a file to, a bunch of work will be done to that file, then the user will be given the option to view some data once the work is done.

To make it look smooth I do all of the work to the file in a seperate thread, then use an AJAX timer/update panel to update the page on the client side as the work progresses server side.

Unfortunately, I ran into a problem when I tried testing it. Running in the VS2005 environment (localhost:port/site/page) it works flawlessly. But when I try to browse to it hosted on IIS5 (localhost/site/image) I get "Cannot access a closed file."

Here's a trimmed up copy of the code:
Code:
Protected Sub page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If Not Me.IsPostBack Then
    'initial load stuff here
  Else
    'initial submit
    If Not Me.Timer1.Enabled = True Then
      If Not fdPresentation.PostedFile Is Nothing And _
         fdPresentation.PostedFile.ContentLength > 0 Then
           'display the update panel
           Me.up1.Visible = True

           Dim del As New del_DoWorkInBackground(AddressOf DoWorkInBackground)
           'Me.ifPresentation is the HTMLInputFile
           del.BeginInvoke(Me.ifPresentation, Nothing, Nothing)
         Else
           Response.Write("Please select a file to upload.")
         End If
       End If
     End If
  End Sub

  Private Delegate Sub del_DoWorkInBackground(ByRef ifPresentation As htmlinputfile)

  Private Sub DoWorkInBackground(ByRef ifPresentation As htmlinputfile)
    Dim m_blStatus As New BulletedList
    Try
      Me.Timer1.Enabled = True
      '...
      [COLOR=red]ifPresentation.SaveAs(SaveLocation)[/color]
    catch exc as exception
      'error reporting
    end try
  end sub
end class

It appears to be some kind of difference between the VS2k5 host and IIS, but I'm not sure of a way to make IIS behave better. Any thoughts, configuration or code whys?

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
most likely file permissions. the user must be able to read/write to the location they are saving to.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Unfortunatly, I don't think that's the problem :(

I put this in right before the ifPresentation.postedfile.SaveAs(SaveLocation)

Code:
Dim f As IO.File
Dim sw As IO.StreamWriter = f.CreateText(SaveLocation & "1")
sw.Write("test")
sw.Flush()
sw.Close()
sw.Dispose()

The test file shows up with the text "test" inside it, and I still get the error on the saveas line.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
is your system trying to access the file in the orginal thread after your save the file? example

1. upload file
2. call background process
|
+->save file
3. access file

if so you might have a race condition which did not appear on your developement box. the main thread is trying to access a file before the background thread has completed its task.

does the ifPresentation.SaveAs have any asynchronosis functions?

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Not that I'm aware of. I have a theory though. My guess is that the htmlInputFile is tied to the post, the VS2k5 web host is holding that post in memory even after the postback event has completed and the response has been sent to the client. I'm guessing the IIS 5 does not retain that chunk of memory, once the response is sent, it releases the memory to maintain performance. This is all just a guess though, I have no way of verifying it. Although I am curious if it would work on IIS 6...

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I didn't manage to find any elegant way to do what I was looking for. So I create a new page and put my AJAX timer and update panel on that. Then I added an IFrame that pointed back to the submission page. The submission page saves the file on post back, then pushed all the other work into a different thread. This still presents a lack of responsiveness while the file is being uploaded, but it allows me to update the interface once the file transfer is complete.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top