INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Settings & close event in C#  (Read 4264 times)

lucian.jp

  • Junior Woodchuck
  • **
  • Posts: 59
Settings & close event in C#
« on: October 01, 2009, 12:23:03 pm »

Hi,

I'm trying to do a little plugin that need to persist the setting. I used Settings.Default, but when restarted Media Center, the setting isn't persisted, the default value is restored. How can I save settings ?

I also noted that using the Notify Track Change and Player state event do not trigger when I close media Center. How can I execute some code when closing the application?

Thanks
Logged

muchadhesion

  • Recent member
  • *
  • Posts: 44
Re: Settings & close event in C#
« Reply #1 on: October 02, 2009, 08:01:00 am »

I've hooked into MC shutdown by overriding Dispose and the WndProc message.

You need to call Save() on your Settings to have them persisted.

Code: [Select]

void FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.Save();
}

        protected override void WndProc(ref Message m)
        {
const int WM_DESTROY = 0x0002;
            // Listen for operating system messages.
            switch (m.Msg)
            {
// The WM_DESTROY message occurs when the application exits
                case WM_DESTROY:
FormClosing(null,null);
                    break;         
            }
            base.WndProc(ref m);
        }

protected override void Dispose(bool disposing)
{
// unhook the upnp stack
if (disposing)
{
FormClosing(null, null);
}

if (disposing && (components != null))
{
components.Dispose();
}

base.Dispose(disposing);
}


You'll also need to manage settings "upgrades", so Settings are migrated if new (minor) versions of MC are installed by the user.

Code: [Select]
// check for upgrade and migrate user settings
                        string version = mcRef.GetVersion().Version;
if (!version.Equals(Properties.Settings.Default.LastMCVersion))
{
Trace("First Execution of MC " + version);
Trace("Upgrading User Settings");
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.LastMCVersion = version;
Properties.Settings.Default.Save();
}
Logged

lucian.jp

  • Junior Woodchuck
  • **
  • Posts: 59
Re: Settings & close event in C#
« Reply #2 on: October 05, 2009, 05:48:44 am »

Thank you very much muchadhesion. It was exactly what I was looking for. Everything works very well now. Teal'c would have given 5 indeeds out of 5.
Logged

pbair

  • Recent member
  • *
  • Posts: 33
Re: Settings & close event in C#
« Reply #3 on: October 23, 2009, 01:22:33 pm »

The following post provides a couple more ways catch the MC Close event:

http://yabb.jriver.com/interact/index.php?topic=15760.msg106918#msg106918
Logged
Pages: [1]   Go Up