English 中文(简体)
Safest way to copy a file
原标题:

I need to merg two PDF files. However sometimes a file might be locked up

I wrote this code, but I m wondering if it s not the smartest solution:

     private static int FILE_LOCKED_WAIT_PERIOD = 1000;
while (true)
                    {
                        // If the file is in use, IOException will be thrown.
                        // If file is not permitted to be opened because of Permission 
                        // Restrictions, UnauthorizedAccessException will be thrown.
                        // For all other, Use normal Exception.

                        try
                        {
                            inputDocument1 = PdfReader.Open(fileToMerge, PdfDocumentOpenMode.Import);

                            break;
                        }
                        catch (IOException)
                        {
                            Thread.Sleep(FILE_LOCKED_WAIT_PERIOD);
                        }
                        catch (UnauthorizedAccessException)
                        {
                            Thread.Sleep(FILE_LOCKED_WAIT_PERIOD);
                        }
                        catch (Exception)
                        {
                            Thread.Sleep(FILE_LOCKED_WAIT_PERIOD);
                        }
                    }
问题回答

You should add a timer so that you sleep for a few clicks before you try the file operation again.

Also you should have counter so you do not wait indefinitely and that you exit after say 15 tries.

Well this depends: 1) Is it a process that is all internal to a system independent of a user? If so you should try to find out what is locking the file and wait for the explicitly. Waiting randomly and then trying over and over again may cause problems on its own.

2) Is it a user that may have the file open? In this case waiting is not helpful since the system could retry all weekend because the user suddenly left for the day. You have no control over user timing. Just tell the user that you cannot do the requested operation because the file is open and have them try again.

Usually waiting for N seconds/minutes is not really a solution. Either you know what the problem may be and poll & resolve the issue or you can t really do anything and just send out notice.

There is no special function to do this. Actually even if this function exists, some process can still easily lock this file between your "lock check" and "file open"





相关问题
Tools to generate reportanalytics feeds from google

Can you recommend any programmable tools/extensions that can be used to generate reports out of google analytics feed on th fly? Essentially some kind of pdf generator from feed, not sure though. Or ...

Asynchronous generation of PDF - error handling

We have some code that uses a third party component to generate a PDF from a URL that we pass it (the URL being a page within our application). In the code, we instantiate the PDF generator and it ...

HTML to PDF vs. Programmatically creating PDF via PHP

I have a PHP application that needs to generate some PDF invoices and PDF timesheets with nice headers/footers. Some Stackoverflow users recommend using TCPDF to create the PDF documents. In my ...

Open PDF Programmatically in One Page View

I have a 2005 Reporting Services report that I display as a PDF through the report viewer control. I would like to know how I can have the PDF be viewed in the "one page at a time mode". Right now it ...

iTextSharp : pdfPTable overlapping with other content

I am using iTextsharp to generate a PDF document from C# application. I am having a PdfPTable to display some tabular data. My problem in the table is overlapping with the other paragraph .Is there ...

(pdflib) Table of contents with dynamic page numbers

I m on pdflib 7 under php5 and I m drawing a statistical report with several sub topics over several pages. Now, I want to include a table of contents on the first page where every sub topic is ...

热门标签