Solved

Getting error while viewing PDF File?

Posted on 2014-01-15
6
307 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

777 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