How can we remove a piece of code from a application after a number of execution? Let me give an example, right now, I have this application SomeSoftware, that takes backup of data every month or so. What it does is, that at the time of installation on client’s machine (on deployment) it creates an entry in database about the date it was installed on, and then at each run it checks something like
var con = InitializeDB.StartConnection(); // start the connection
var cmd = con.CreateCommand();
var cmdOther = con.CreateCommand();
// check if its the first run of the software
#region onlyUsefulForFirstTime
cmd.CommandText = "SELECT Count(*) FROM Misc WHERE ItemName='FirstRun'"
if (Int32.Parse(cmd.ExecuteScalar().ToString()) == 0)
// it is the first run, add the entry
cmd.CommandText = "INSERT INTO Misc(ItemName, Val) VALUES('FirstRun'," + DateTime.Now + ")";
else
#endregion
// it is not the first run
cmd.CommandText = "SELECT Val FROM Misc WHERE ItemName='LastRun'";
cmdOther.CommandText = "SELECT Val FROM Misc WHERE ItemName='FirstRun'";
// now after this, we compare the value of LastRun and the date in FirstRun, if it's more than a month, we take the steps to backup the database
what’s happening currently is that at each start up of the application, it is executing the code inside region onlyUsefulForFirstTime, though its only useful for first time run. If client were to run this application 10 times in a day, it would be a total waste to keep letting this code execute. I know, I need to check somewhere if its first run or not, so to add the value, but after that its just waste. So, is there any way I could remove this code, and not exactly remove, but make the code in onlyUsefulForFirstTime not execute again, or something like that, well! you get the idea!
ps : I am not asking about other methods for backup or other strategy to backup, I know there are others, but that’s not the question!
Add a key to the Windows registry, indicating that the code has already run. Check this registry key on startup of your application.
Alternatively, you can use an application setting: