views:

383

answers:

0

I am trying to implement a progress bar to monitor a file upload. The file is uploaded in an iframe and the server side C# code behind implements a buffer to upload the file in chunks. It uses a while loop to do this and upload performance seems to be fine with ASP.NET 3.5, and I really do not want to use a 3rd party control or a complex upload scheme. Every time the processing goes through the while loop, it updates a session variable with the number of bytes which it has processed and the total file size.

On the client side, the button which implements the iframe upload form submit also starts a Javascript setInterval(...) to a function which uses XMLHttpRequest to another asp.net page whose sole function is to read those session variables and Response.Write them out. These should get returned to the callback function and used to update the progress bar.

Conceptually this works and the flow is possible. I know this by putting a breakpoint in my upload handler to stop uploading and putting another breakpoint in the 'ping' page. I can go back and forth in the debugger and watch the session variables being set and read.

Unfortunately, this does not quite work in reality without the breakpoints. The ping page will go out and not return. This happens until the upload is finished, at which point it will happily work away getting one the final results.

Since these are two separate page requests, I am assuming that what I have are two separate threads, both trying to use the same session variable. Since these are spawned by IIS, I have no handles to these threads. The upload process is probably using the session variables so much that the ping page cannot get it. Also, I have had the system lock up both the browser and IIS. I have tried to read up on thread synchonization, but I do not understand this well enough to implement it between threads for which I have no handles.

Would anyone be kind enough to show me some C# code on how to do this? I can find example code, but they all create the two threads and thus have the handles. I can get the current thread in either one of the pages, but the pages have no access to the each other's threads.

Thanks you!