StaticVarAutoSave

Is it possible to save persistent variable to PersistVars.bin only one on trigger e.g. after ParamTrigger?

1 Like

You can set that option to 1 after Paramtrigger then request timed refresh to say 2 seconds and turn it back to zero

Yes, but when the chart is refreshed quickly, AutoSave will switch to false before 2 seconds.
Why is this important to me? I want to make sure that the variables important to me are saved before the next session, in case of power failure or the Amibroker crash.

if(ParamTrigger("increase","increase"))
{
StaticVarSet("lot",Nz(StaticVarGet("lot"))+1,True);
SetOption("StaticVarAutoSave", 2 );
}
else
SetOption("StaticVarAutoSave", 0 );

Should i use some timer?

@jcorniko, as suggested by @Tomasz you should use a forced refresh.

I think something like this could work:

if(ParamTrigger("increase","increase"))
{
	StaticVarSet("lot",Nz(StaticVarGet("lot"))+1,True);
	SetOption("StaticVarAutoSave", 1 );	
	RequestTimedRefresh(2); 
}
else {
	if (Status("redrawaction") == 1) {
		// Disable ONLY when triggered by RequestTimedRefresh
		SetOption("StaticVarAutoSave", 0 );
		RequestTimedRefresh(0);
		_TRACE("Resetting auto save to 0");
	}
}
// // Check if static vars are otherwise saved when exiting normally or killing Amibroker
if(ParamTrigger("increase NO SAVE","increase NO SAVE"))
{
	StaticVarSet("lot",Nz(StaticVarGet("lot"))+1,True);
}

Title = "Lots: " + StaticVarGet("lot");

The refresh timer (and resetting the autosave interval to 0) should go off only after 2 seconds and ignore "normal" refreshes triggered by actions on the chart.

To see if it works properly try also to use the "NO SAVE" trigger and check what happens if you kill the AmiBroker process (in such a way that it cannot persist the static vars as it does when you close the application regularly).

1 Like

thanks. I try code the same like yours, and it works.I checked when the file (PersistVars.bin) modification time changed.

@jcorniko - as I wrote in my FIRST POST you need to do RequestTimedRefresh

@beppe The code suggested is way too long for what it is supposed to do.

Correct code is this:

if(ParamTrigger("increase","increase"))
{
     SetOption("StaticVarAutoSave", 1 ); // save in 1 second
     RequestTimedRefresh( 2 ); // timed refresh after 2 seconds
}

if( Status("redrawaction") == 1 ) 
   SetOption("StaticVarAutoSave", 0 ); // turn off periodic auto-save

FYI: You don't need to do RequestTimedRefresh(0).

Also permanent static variables are automatically saved anyway, on process exit.

Besides there is a hidden feature (passing negative value) that allows to do the one-shot autosave in just one line

// this hidden feature is going to be working as intended in 6.42+ 
if( ParamTrigger("trigger autosave", "trigger autosave" ) ) 
    SetOption("StaticVarAutoSave", -1 ); // one-shot auto-save
6 Likes

thanks Tomasz, but are you sure? I can see that with -1, PersistVars.bin changes every 1-2 seconds.

1 Like

OK, since it is hidden it wasn't tested that much so maybe there is an issue with it. Please use longer code then until I check & fix it.

Update: I re-checked it and indeed negative value in 6.40 works the same as positive value (there is an abs() function called), so it saves fine but does not stop periodic saves. I already fixed it in my development version.

3 Likes

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.