Search code examples
c#.netwpfpixelsense

Microsoft Surface: Where to define methods that have only be called during startup?


I have some methods, that should only be called during startup of the application. For now, I placed the methods in OnApplicationActivated:

private void OnApplicationActivated(object sender, EventArgs e)
{
   Sound.loadSounds();             
   GetLocalProjects();
   GetProjects();            
}

But OnApplicationActivated isalso called, if the application was in the "pause" state (the shell is displayed) and is reactived. How can I avoid this? I want to call these methods only during startup.


Solution

  • I don't know anything about Microsoft Surface so I can't tell you if there are some other event that could be hooked up, but wouldn't an easy fix be to use a boolean instance variable to check whether initialization has been done or not?

    bool _isInitialized = false;
    private void OnApplicationActivated(object sender, EventArgs e) {    
      if( !_isInitialized ){
        Sound.loadSounds();
        GetLocalProjects();
        GetProjects();          
        _isInitialized = true;   
      }
    }