Solved

Getting error while viewing PDF File?

Posted on 2014-01-15
6
308 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

821 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