Author Topic: Read TextFile Odd Behavior {Variables Not Flushed From Memory Inline Completion}  (Read 3752 times)

Starblue7

  • Full Member
  • ***
  • Posts: 131
When I go in to edit a C# Inline Function that reads a text file, I can read the contents.
If I execute the same VA Command which kicks off the Inline Function, it doesn't read the file contents.

I noticed this after I had the Inline Function create a text file with no variables.
Kicking off the Inline Function returned a form with no variables (which was correct).

I then deleted that file and replaced it with a copy of the same file, but this time with variables placed inside it.
In kicking off the VA Command to run the Inline Function, the form pops up but doesn't populate the variables from the file.
Yet, when I go to EDIT the VA Command and run the inline function by testing it, it operates as normal and reads in the variables from the file to the form.

Anyone have any ideas about something like this?  Rather weirder behavior.  I'm even clearing controls, and disposing form on form close event.  That doesn't make a different.
« Last Edit: June 13, 2023, 07:04:01 PM by Starblue7 »

EricM

  • Newbie
  • *
  • Posts: 13
  • VA user since 2017
Re: Read Text File Odd Behavior
« Reply #1 on: June 13, 2023, 01:25:25 PM »
You can dig deeper in debugging your C# code by printing your variable's information to the VA log at strategic points in your code.  If it doesn't print at all, it didn't make it that far.

Also, look out for different invisible line ending characters and mismatched text encodings. ::)

However, if the VA get-user-input forms are not enough for your scenario, I think you should consider making a VA plugin in Visual Studio.  VA ships with an uncompiled sample plugin project to play with.  Using Visual Studio you'll have intelisense, debugging, and easy win forms too.

Starblue7

  • Full Member
  • ***
  • Posts: 131
Re: Read Text File Odd Behavior
« Reply #2 on: June 13, 2023, 03:36:37 PM »
Yeah.  I don't think that's the issue.

I suspect it's something to do with VA and the code releasing from memory the prior values between runs.

The Inline Function worked when:
1. Creating the text file if it didn't exist
2. and simply reading a text file and values from it.

Once I introduced the Stream Writer routine to WRITE to the newly created text file:
1. The file with variable tags was created with the empty values
2. The code read the file and opened the form with no values in the controls (Correct Result)

Then:
1. Closed the form with dispose() and cleanup
2. VA Command closed
3. Manually deleted the created file and replaced it with a copy with actual variable values
4. Ran the Inline Function again and it found the file (i.e. didn't create a new one automatically)
5. Supposedly read the file.
6. Yet the form had no values in it from the text file.
7. Yet, when I go in to EDIT mode of the command and Inline Function and run it THAT way, the form was populated with the variables.

That's why this is weird.

Starblue7

  • Full Member
  • ***
  • Posts: 131
Re: Read Text File Odd Behavior
« Reply #3 on: June 13, 2023, 05:50:32 PM »
Just to add.

If I close out of Voice Attack and re-open and run the Inline Function again, it will import/SHOW the data in the form fields correctly.

Even on form close within the Inline Function, using Dispose() / Controls.Clear() and even pushing a GC.Collect();
doesn't seem to make any difference in the dealing of memory.
The 'Retain Instance' is not enabled, and I've chosen to wait for the inline function to finish before continuing, to which I've confirmed is happening.  So I feel it's some memory issue.  I'm sure it could be an issue in my code, but it's not quite obvious what else I need to do to clear the memory state of the inline function.

I'll take a closer look at stream/thread management, but again so far nothing obvious at the moment.

Starblue7

  • Full Member
  • ***
  • Posts: 131
Re: Read Text File Odd Behavior
« Reply #4 on: June 13, 2023, 07:02:29 PM »
Right... My fault.  I'm using a Public Static Global to store/use Global Variables (WHICH I KNOW IS BAD PRACTICE...) and I just wasn't resetting those global variable values on form close.
So, those values simply persisted in memory and were used and superseded any values that the String Reader was reading from the file..

I don't know why those variable values have to persist in memory and can't be flushed after I close/end the Inline Function within VA, but they do, and I suspect they're maintained in the VA memory buffer.  (As only closing out and re-opening VA previously solved the issue).  I kinda thought that selecting the 'Retain Instance' would preserve that memory, and unchecking it would flush that inline function's memory use upon the end of the Inline Function.

I will get to some point and refactor my code.  But I'd like to have variables that can be used across Classes easily.
Especially if I'm reading variables from a text file and I want to push those values into the Form Fields of the form that I call up from the FORM class.  I suppose I could simply just put all that code within the Form Load Event to keep it all within the Form Class, and then call other functions and send specific arguments to them on a case by case basis.

Disclaimer:  I'm not a 'professional' coder, so please try not to whip me so hard at any archaic pagan coding practices.  hehe

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4644
  • RTFM
Do you have example code to replicate this?

Starblue7

  • Full Member
  • ***
  • Posts: 131
Sorry for delayed response pfeil.
I've since started to refactor my code and make improvements to it, so it really isn't in the form it used to be.

Once I finish, I'll try to replicate the situation above and provide a code snippet for it.