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:
Two notes:

Timer.Interval is the amount of time between ticks, so, you would want this at either 1000 or 2000 to match your 1-2 seconds request (its in miliseconds).

The second point is regarding the approach. Rather then checking for only 12 seconds you should just check for existance. Who's to say that the system isnt running slow and generation takes 14 seconds one day, this would throw your whole approach off.

Try an approach similar to this (psudo-code only):
VB.NET:
While Not FileExists(FileName) = True
Loop

RenameFile(FileName)
Redirect(FileName)

The immediate problem that I see is that often a file EXISTS before it is completed. Whatever is generating these PDF's likely starts the file to disk long before it is completed. Your program will see this file being there and try your move routine. This will throw an access exception as you'll be trying to move a file another application has open for writing.

Do you have any more details about how these pdf's are being generated? Do you control that function?
 
Thanks Raven

The pdf file is created by a printer driver called PDFCreator. It is a print of a MS Access report. This process will be used by many users so I can't really query the PDFcreator process.

Any advice is again appreciated!

Dave
 
Another point Raven

How do I build your example code into the timer interval?

Dave

In the form design mode, drag a timer onto your form. It should load into the control window just below your design window. Double click it when it appears and you should be given the Timer1_Tick event structure.
 
It's in the 'Components' section (of course also in 'All' section)
 
I don't seem to have a components section, standard, data, validation etc..but no components..

Is it becuase it's a web app?

Thanks....

Dave
 
Is it becuase it's a web app?
I missed that since you posted in VB.Net section and not ASP.Net section... That complicates things somewhat.. you can 'wait and poll' in code by using:
VB.NET:
Do
  System.Threading.Thread.Sleep(1000)
  If [file exists] Then Exit Do
Loop
...but that would leave user waiting for reponse page to load all this time.

Better but much more complicated is to use multithreading with session variable and a 'waitpage' that postback every now and then to check if the other thread has notified session that it is finished. This 3 pages article at SitePoint describes this approach.
 
Raven

You were right about the file not existing even though it has been written to disk. The file exists command returns true but then it can't be renamed!

Any alternative suggestions?

Thanks again

Dave
 
The way I see it gets complicated here. Someone might be able to enlighten us both on a "is file complete" method, but I know of no such thing.

My approach (initial thought at least) would be to scan the directory every X seconds (10?). Store the name and byte size of the files in the directory in an ArrayList. Scan again (every 10 as stated) and check each file for size again. If the size hasn't changed the file should be complete and you can rename that one and remove it from the ArrayList.

You might be able to trap the exception of FileCannotBeAccessed (or whatever it is) and move to the next item in the list so that the failed item would be tried again next loop.

Let me know if you need examples of any of that, or anyone else feel free to help if I'm over complicating..
 
Thanks again Raven

I think I might change track completely. The pdf file in question is a snapshot format file output from access.

It then gets printed to the PDF print driver to convert it to PDF for viewing in the client browser (hence the problem in this thread).

The alternative is to leave as a snapshot format and request all the client PC's to install the snapshot viewer. You can probably see why I wanted to convert it to PDF but I'd like the expert's views.

Any thoughts are appreciated as I'm a novice!

Dave
 
My advice would be to give my approach above a try.

TimerLoop
'Check each file from last tick
For Each File In Array
Check CurrentSize, if same, rename.
Next

'Clear the Array
Store each filename/size in array
Loop

This would enable you to move the files only after their file size became static (implying completion).
 
Yeah you're right Raven...but.....

it's a web app and I can't add the timer component so what's the code for the tick event when it's done in a web form?

Also, I'm poor on arrays so any help on that code would also be appreciated...sorry!

Dave
 
Back
Top