Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Getting error while viewing PDF File?

Posted on 2014-01-15
6
Medium Priority
?
327 Views
Last Modified: 2014-01-15
I'm viewing a PDF File using the Process.Start(fileName) method. My problem is that I keep getting the following error message:

      "Process must exit before requested information can be determined."

I get this message on the following line of code:

                                s = Process.Start(strFileName)

Open in new window



The weird thing is when I run it in debug mode...it works fine. Here is my code. It seems pretty straight forward as to what I need to do.

            If Not dt Is Nothing Then
                If dt.Rows.Count > 0 Then
                    For Each row As DataRow In dt.Rows
                        If Not IsDBNull(row(0)) Then                            
                            Dim fByte As Byte()

                            If Not IsDBNull(row(0)) Then
                                fByte = row(0)
                                Dim s As New Process
                                Dim strPath As String = "C:\SENSIT Calibration"
                                Dim strFileName As String = strPath & "\" & gEquipCode & Now.Year() & Now.Month() & Now.Day & ".pdf"

                                If (Not System.IO.Directory.Exists(strPath)) Then
                                    System.IO.Directory.CreateDirectory(strPath)
                                End If

                                File.WriteAllBytes(strFileName, fByte)
                                s = Process.Start(strFileName)

                                Do Until s.ExitCode = 1
                                    'Wait for User to close file
                                Loop

                                If s.ExitCode = 1 Then
                                    If Directory.Exists(strPath) Then
                                        For Each fileName As String In Directory.GetFiles(strPath)
                                            File.Delete(fileName)
                                        Next
                                    End If

                                    Directory.Delete(strPath)
                                    s = Nothing
                                End If
                            End If
                        End If
                    Next
                End If
            End If

Open in new window

0
Comment
Question by:BlakeMcKenna
  • 3
  • 3
6 Comments
 
LVL 40
ID: 39782787
The file write is probably not finished.

Put a small delay between WriteAllBytes and the start of the process. Application.DoEvents might be sufficient. Otherwise, call Thread.Sleep to add a small delay.
0
 

Author Comment

by:BlakeMcKenna
ID: 39782801
I tried Application .DoEvents and that didn't work. BTW, this is a Windows app.

                                File.WriteAllBytes(strFileName, fByte)
                                Application.DoEvents()
                                s = Process.Start(strFileName)

Open in new window

0
 
LVL 40
ID: 39782955
Then try with Thread.Sleep, specifying a delay in milliseconds. The following will wait for half a second. Increase the value as needed:

Thread.Sleep (500)

There is a default with that. On computers that are faster, or if the application lives for a few years (computers will be faster eventually :-)) the delay will not be enough. So you need to add more than what you see as a good value.

Does the code works if you execute it manually line by line in debugging mode^

But looking at your code more carefully, I see something else.

Do you really want to create a file and open it for each row in your DataTable? Trying to open a file on each pass in the loop can overload the application that is used to read the file. Some applications cannot have more than one copy running at the same time.

What if you have a first loop that writes the files, and then another one that opens them afterward?

Is it possible to have duplicates in gEquipCode & Now.Year() & Now.Month() & Now.Day, and so have many files with the same name^
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:BlakeMcKenna
ID: 39783086
James,

I removed the For Loop even though there will always be just 1 record and 1 PDF file to open. I increased Sleep(500) to Sleep(2000) and that still didn't work.

When I step thru the lines of code...it works perfect every time. I have attached the new code. BTW, the PDF file does open with no problem. I just get that error message everytime.

            If Not dt Is Nothing Then
                If dt.Rows.Count > 0 Then
                    Dim row As DataRow = dt.Rows(0)
                    Dim strPath As String = "C:\SENSIT Calibration"
                    Dim strFileName As String = strPath & "\" & gEquipCode & Now.Year() & Now.Month() & Now.Day & ".pdf"
                    Dim s As New Process

                    If Not IsDBNull(row(0)) Then
                        Dim fByte As Byte()

                        If Not IsDBNull(row(0)) Then
                            fByte = row(0)

                            If (Not System.IO.Directory.Exists(strPath)) Then
                                System.IO.Directory.CreateDirectory(strPath)
                            End If

                            File.WriteAllBytes(strFileName, fByte)
                            Thread.Sleep(2000)
                            s = Process.Start(strFileName)

                            Do Until s.ExitCode = 1
                                'Wait for User to close file
                            Loop

                            If s.ExitCode = 1 Then
                                For Each fileName As String In Directory.GetFiles(strPath)
                                    File.Delete(fileName)
                                Next

                                Directory.Delete(strPath)
                                s = Nothing
                            End If
                        End If
                    End If

                End If
            End If

Open in new window

0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 2000 total points
ID: 39783232
Reviewing the thing, I come back to square one. I fixed on your "I get this message on the following line of code" statement and I concentrated on that statement. I think that the problem is on the following line.

There is no ExitCode until the process has exited. When you run step by step, I suppose that you close the viewer before going into the Do Until. The ExitCode exist then. I bet that if you leave the viewer running, you will get your error on the Do Until.

The right way to wait for a process to finish before going on is to call its WaitForExit method. s.WaitForExit should thus replace your loop on the ExitCode.
0
 

Author Closing Comment

by:BlakeMcKenna
ID: 39783313
James...that was it! I needed to insert the "s.WaitForExit" line after the process started and that eliminated the error.

Thanks for your help!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question