Keep variable in memory until system reboot

Asked

Viewed 358 times

4

There is a way to keep a variable in the memory of the machine until it restarts?

My application made a change in the system and sent the message to the user to restart the machine, to prevent any problem I have to detect if the machine has already been restarted even if close the program and start again.

I figured maybe I could use to create an address in memory marking that the machine needs to restart and store in the application settings the address of that variable, even if they restarted the program it would test if the position in memory indicates that it needs to restart...

As I have little knowledge in the "lower level" area of C# I don’t know if it would actually store the variable or if there is a correct form for it.

2 answers

7

Marcus, as Maniero pointed out, you can’t keep a variable in memory, but you can save a flag stating that the system needs to be restarted, perhaps the easiest solution is to change the app.config.

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["Restart"].Value = "true";
config.AppSettings.Settings["Data"].Value = DateTime.Now.ToString("o");
config.Save(ConfigurationSaveMode.Modified);

if you need more data, you can choose to store a JSON file in some system folder (the example below uses the Newtonsoft.Json):

var dados = new { MustRestart = true, Data = DateTime.Now, Foo = "Hello", Bar = "World" };
var json = JsonConvert.SerializeObject(dados);
System.IO.File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + "\dados.json", json);

If you have some kind of paranoia and want to prevent the user from modifying these values, you have two options, to store this data using Sqlite with SEE(Sqlite Encryption Extension). you can see more about.:

finally you can send a request to a WebAPI and this will update a flag informing that the machine needs to be restarted, this approach has two negative points, the client needs an internet connection and you need to implement and host the WebAPI.

As to whether the machine has been restarted, you can read the Windows records as follows, then just scan the entries.:

var eventLog = new System.Diagnostics.EventLog(); 
//eventLog.Source = ""; O ideal que informe o nome do Log que possui os eventos de inicialização do sistema.
foreach (var entry in eventLog.Entries) 
{
   Console.WriteLine(entry.Message);
}
  • I’ll take a look, thanks. I am currently using this feature: http://stackoverflow.com/questions/1474293/how-to-programtically-find-the-last-login-time-to-a-machine it saves the date in a file and if this date has remained unchanged it still requires reboot, but it would give a false positive if it simply left the user, but I do not have so much "paranoia" as to kkkkk, Thanks and friend.

4

I find this a little strange, it seems to me that the solution to the problem should even be other. But answering the question, it is neither possible in C#, nor in any language to keep application data in memory when it is not running.

But you can get the same result by keeping data in a file. What has nothing low level.

  • is a change in windows source, as I do not know how to force windows to recreate the cache of sources ordered the user to restart. I imagined that it was possible to keep a variable in memory because I always see in C the codes give a type of Free(); then I imagined that maybe having the address in memory, maybe I could recover the variable even with the closing of the program, even if I saved it to a file I have no idea to detect if the system has been restarted since last change.

  • maybe if there is some temporary value in windows that changes at each computer startup is already a way to detect... Hmm, maybe the PID of the System process, can let me know if the same process has in all versions of Windows (mine is win 10)?

  • 1

    There is the Windows registry. But that’s a lot of complication. So maybe you should ask another question. You found an odd solution to a problem you had and you wanted information about it. It would be better to ask about the real problem and get better solutions to change the source. A new question with details about what you want can be more productive to have something more effective.

  • 1

    @Marcusandré In this case it is probably case of another question even. Installation of source does not need to reboot if it does correctly. How are you doing such an installation? In fact, a well-made application simply doesn’t touch the system’s sources. There is problem on top of problem there. (the worst is that there are people who use font even for barcode, instead of doing it by the application). It is suggested, when asking a new question, to explain in detail.

  • @Marcusandré To use font without making a fuss on the user system: https://msdn.microsoft.com/en-us/library/system.drawing.text.privatefontcollection(v=vs.110). aspx

  • I am aware that most font installations do not need to restart the system, it turns out that my case is somewhat peculiar if it is a source protected by windows in which I need to modify, in case the MS Gothic and MS Meiryo, I have already contacted microsoft the best solution they managed to fix me was my current method that requires restarting the machine to apply the changes.

  • By the way, the font I could actually load externally, but it’s like I said the case here is kind of peculiar is not my program who will read the font but rather a certain game that only supports SJIS characters, I am then making a modification in the source that such game uses to be able to put our characters (á é í ã ç), using the half-width of katakana/hiragana. So I guess there won’t be any trouble. Unfortunately the game has a DRM and even if I didn’t have I’m not there for such feat of changing the encoding in which the game works.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.