Timer and Loop

davebhoy

Active member
Joined
Aug 22, 2006
Messages
26
Programming Experience
1-3
Hi all
Once again I'm looking for someone's invaluable help!

My web application exports a report to pdf which can take up to 12 seconds to complete and create a pdf file. Then I rename the file and redirect the web browser to show the file.

What I think I need to do is to check for the file every 1 second for 12 seconds using a do while loop. I'm new to coding loops and new to timers so please help! I've made a stab at it as follows:

VB.NET:
'Insert time delay to allow for print conversion to pdf
Dim timer As System.Timers.Timer = New System.Timers.Timer
timer.Interval = 12000
timer.Start()
'Rename the report file to the user
Do While timer.Enabled = True
Loop
If My.Computer.FileSystem.FileExists("C:\Test.pdf") = True Then
My.Computer.FileSystem.RenameFile("C:\Test.pdf", "Test_user.pdf")
Else
'Loop again
End If
Response.Redirect("C:\Test.pdf")
THANKS IN ADVANCE!

Dave
 
Last edited by a moderator:
..............also, on looking at the windows explorer while processing it seems to create the file in the full size from the start..!
 
Oh man! Forgot you mentioned that earlier =) I usually stay away from the webapps as I'm more a forms guy. I'll play on the web side a bit and see if I can see an alternative.
 
Sounds like a valid approach. Can you trap that error, copy to clipboard, and paste here so I can see what your seeing? My guess is you could, for that specific error, just continue past, and it would work next time.
 
If in your code you have a Try..Catch then in the catch just put a stop and you can then mouse over ther exception variable and get the error to copy. That's how I go about it. However you normally get it do that =)
 
Here ya go Raven

System.IO.FileNotFoundException was unhandled by user code
Message="Could not find file '\\Campserv01\company shares\LA
 
Raven

Here's the code and it works using the goto area command but somethings it hangs for a while so maybe I should use a loop and a timer to retry when it catches the error?

'Delete the existing report file
If My.Computer.FileSystem.FileExists(path & str_rptname & " - " & struser & ".pdf") = True Then _
My.Computer.FileSystem.DeleteFile(path & str_rptname & " - " & struser & ".pdf")
'Rename the file to the user
rename_file:
Try
My.Computer.FileSystem.RenameFile(path & str_rptname & ".pdf", str_rptname & " - " & struser & ".pdf")
Catch ex As System.IO.FileNotFoundException
GoTo rename_file
End Try
Response.Redirect("reports\" & str_rptname & " - " & struser & ".pdf")
 
Well, first of the hanging is likely because of the:

VB.NET:
[SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]My[/COLOR][/SIZE][SIZE=2].Computer.FileSystem.RenameFile(path & str_rptname & [/SIZE][SIZE=2][COLOR=#800000]".pdf"[/COLOR][/SIZE][SIZE=2], str_rptname & [/SIZE][SIZE=2][COLOR=#800000]" - "[/COLOR][/SIZE][SIZE=2] & struser & [/SIZE][SIZE=2][COLOR=#800000]".pdf"[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.IO.FileNotFoundException
[/SIZE][SIZE=2][COLOR=#0000ff]GoTo[/COLOR][/SIZE][SIZE=2] rename_file
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Try
[code]
[COLOR=Black]
This is going to enter a fast loop while the file isn't able to be moved...try-fail-try-fail over and over. This works, but what you really likely want is to display a page that is a "please wait while processing" rather than just a seemingly hung page.

That aside, does the method work as you have it here? Waiting on the error to resolve once the creator releases the file then move it?[/COLOR]
[/COLOR][/SIZE]
 
Raven

It seems to work most of time but hangs now and again. Would a delay in the loop fix this? Any code suggestions?

Thanks for all your help and patience on this...

Dave
 
Someone with a bit more experience in the ASP/web side might have to assist here. I would think though, that on this page doing the processing you could have a "Processing..." show while the loop is executing and then once its good, move to the redirect page.

Have you tried that? Or, does the "hanging" prevent you from properly displaying the page?

Perhaps in your code load the page, show the "Processing..." dialog, then start the loop you have now (the try-catch-goto). This way the dialog is up the whole time, then, when finally finished it redirects.
 
Thats great, thanks again

It may be the server that causes the long wait sometimes becuase other times it opens within about 5 seconds which is what I'd expect
 
Likely a combination of things. Disk being accessed by other programs...several "orders" comming in at once, etc etc. Give the "Processing.." page a go and see what you think with that showing while the loop processes.

Let us know how it goes!
 
Yes!

There were two possible errors I needed to catch

1) File not found
2) File in use

When I catch both errors it seems to work fine!

Many thanks again, I owe you a few pints for that!

Dave
 
Great to hear is working for you now. Best of luck on your future development.

As for the pints, anything import will be just fine ;-)
 
Back
Top