Text Stream Exit

I have code that reads text from a file and inserts it in a database. The code iterates through five lines of the text file inserts them in the database and then exits the subroutine, skipping the text stream close statement and not raising any errors.

[code] dim fldrFile as FolderItem
dim txtStrm As TextInputStream
dim ConvText As String
dim dr As DatabaseRecord

fldrFile = GetFolderItem(fldrFilePath, 3)

If fldrFile <> Nil Then

txtStrm = TextInputStream.Open(fldrFile)
txtStrm.Encoding = Encodings.WindowsLatin1

While Not txtStrm.EOF
  ConvText =  txtStrm.ReadLine
  ConvText = ConvertEncoding(ConvText, Encodings.UTF8)
  dr = New DatabaseRecord
  dr.Column("FilesListed") = ConvText
  App.FileList.InsertRecord("Files", dr)
  If App.FileList.Error Then
    MsgBox("Database error: " + App.FileList.ErrorMessage)
  ElseIf txtStrm.ReadError Then
    MsgBox("TestStream Error: " + Str(txtStrm.LastErrorCode))
  End If


End If[/code]

I am baffled and at a loss. Any ideas would be appreciated. Thanks.


Do you have “Break On Exceptions” turned on?

Yes and there is no break when it jumps out of the subroutine.

put a break on the READLINE and watch the variables… perhaps your file really only has 5 endoflines, and its 5 HUGE “paragraphs”
or there is a Ctrl-Z embedded in the file that is causing it to think it reached EOF.

I guess you’ve tried stepping through the code as it imports each line?

Tried stepping through the code. It just hops out of the subroutine after five lines. It skips the txtStrm.Close statement. I tried changing the sorting of the 771 line file, but it still stops after five lines. It does not appear to be the file. It is something in the database or IDE.

I have now found that it has something to do with updating the database from a thread. After a certain interval the thread dies and the program is back in the calling subroutine. I have found that if I increase the thread’s priority that it lasts longer and adds a greater number of records to the database (10 instead of 5). I have no idea why the thread is dying.

Ah, that makes sense. Are you keeping a reference to your thread somewhere? Because if you don’t, if you let the thread go out of scope, it will die.

How do I “keep a reference to the thread?” How does it go out of scope?

Suppose you start a thread like this from a button:

dim t as new MyThread

The Thread will start, but as soon as the code for the button ends, the Thread in t will go out of scope and terminate. Instead, you would create a property somewhere more permanent in, say, your Window or a Module, then assign the Thread to that. If a window, and you don’t close it, there will be a reference to the Thread and it will not go out of scope. If a Module, it won’t go out of scope until you quit your app.

An object goes “out of scope” when there is no longer a reference to it anywhere. In other words, as soon as you no longer have a way to access that object, it no longer exists.

Thanks. I did what you suggested, but the behavior is the same. Actually, when the code hits the break point I have set after the database load thread execution, the thread is still in existence in either case: local or app level thread instance. So, for some reason the program is exiting the thread routine when apparently a context switch occurs, not because the thread is ending.

Can you post the code that starts the Thread?

App.RecrsFldrs and App.FrList are application level property thread variables.

[code] '2_0 Update
ProgressWheel1.Visible = True

If FromFile.Value = False Then
App.RecrsFldrs = New clsRecurseFolders
'Dim RecrsFldrs As New clsRecurseFolders

'2_0 Update
App.strFolderPath = FilePathLoc.Cell(0,1)
'RecrsFldrs.Priority = 10

App.FrList = New clsFromList

'2_0 Update
App.strFolderPath = FilePathLoc.Cell(0,1)
'FrList.Priority = 10

End If

'2_0 Update
ProgressWheel1.Visible = False

Any chance this code is running more than once, or you are setting App.FrList to nil or new clsFromList elsewhere?

Run Event example:

'2_0 Update RecurseFolders(App.strFolderPath)

Code in the thread:

[code] dim i, C as Integer
dim fldrPath, fldrFile as FolderItem
dim strInfo As String
dim dr As DatabaseRecord

fldrPath = GetFolderItem(fldrSearchPath)
C = fldrPath.count

For i = 1 to C
fldrFile = fldrPath.trueItem(i) //uses trueItem so that aliases aren’t resolved

If IsFolder(fldrFile) = True Then //method to check for folders
  RecurseFolders(fldrFile.GetSaveInfo(fldrFile)) //Recursive call
  IF File_Type(fldrFile) = "pdf" Then
    '2_0 Update
    dr = New DatabaseRecord
    strInfo =  fldrFile.GetSaveInfo(fldrFile)
    dr.Column("FilesListed") = EncodeBase64(strInfo, 0)
    App.FileList.InsertRecord("Files", dr)
    If App.FileList.Error Then
      MsgBox("Database error: " + App.FileList.ErrorMessage)
    End If
  End If
End if


Sorry for the double information. Plainly, I am not a regular forum user.

I’m afraid I’m at the end of the help I can offer without seeing the project. Any chance of that?

I can let you have a hobbled copy. The actual work from the file list won’t work, because it requires an installed copy of dtSearch, but it will process through loading the database all right. What is a good way to get it to you?

Place it somewhere that I can download it (DropBox, web server, etc.), then send me a link. Do it in a PM if you prefer to keep it private.

I got the project, and it runs, but do I need the input file too?