Solved

Getting error while viewing PDF File?

Posted on 2014-01-15
6
302 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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:BlakeMcKenna
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Zoom web page in asp.net 2 34
Separate a number and letter from a string 25 23
Duplicate a row 2 28
VB.NET HttpWebRequest 12 30
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now