Solved

Getting error while viewing PDF File?

Posted on 2014-01-15
6
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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