Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Cleanly Killing The Itunes Com Object
vizskywalker
post Oct 16 2007, 05:31 AM
Post #1


Techno-Necromancer
Group Icon

Group: Members
Posts: 1,018
Joined: 13-January 05
From: The Net
Member No.: 2,127



So, I'm working on a media server that interfaces with iTunes to play my music for me. I've run into a small nuisance, but not something critical. To work with iTunes, a C# application needs to instantiate an iTunes COM object. This actually forces iTunes to open. Now, if a person were to attempt to close iTunes, the C# application would receive an AboutToPromptUserToQuit event. If the iTunes COM object is not destroyed and dereferenced so that iTunes thinks it can cleanly exit without stranding any other application, the user will be prompted to confirm the quit. I have not found a way to handle the event properly to prevent this prompting. I have properly handled the event to do things such as output messages, but my attempts to destroy the object fail. I have tried setting the object to null, and hoped the Garbage Collector would clean it in time, but that didn't work. So then I tried forcing the garbage collector, but it still didn't work. Anyone have any ideas, or has anyone tried anything themselves that has worked?

~Viz
Go to the top of the page
 
+Quote Post
vizskywalker
post Oct 17 2007, 05:45 PM
Post #2


Techno-Necromancer
Group Icon

Group: Members
Posts: 1,018
Joined: 13-January 05
From: The Net
Member No.: 2,127



So, I managed to stumble across the answer just now. The steps are:
1) Use Marshall to release the object
2) Null the reference
3) Run the Garbage Collector to ensure the reference is gone

The code is as follows:
CODE
Marshal.ReleaseComObject(object o);
o = null;
GC.Collect();


Now if only I could figure out a way to cause that code to run when the program stops running.

~Viz
Go to the top of the page
 
+Quote Post
ScepterDonFetti
post Dec 30 2007, 07:56 AM
Post #3


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 14
Joined: 30-December 07
Member No.: 27,229



Try putting the code under the application.run(new form); statement in the static main() method of your program.cs file like this...

CODE
static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());

            // Be sure to use the fully qualified name for object o so that C# can find it from here.
            Marshal.ReleaseComObject(object o);
            o = null;
            GC.Collect();
        }
    }


or, you can put it in the closed or closing event of your main form like this...

CODE
public partial class MainForm : Form
    {

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {

            // This if statement is optional unless the form is an MDIChild or has an Owner.
            if (e.CloseReason == CloseReason.ApplicationExitCall
                  || e.CloseReason == CloseReason.UserClosing
                    || e.CloseReason == CloseReason.WindowsShutDown
                       || e.CloseReason == CloseReason.TaskManagerClosing
                          || e.CloseReason == CloseReason.None)
            {
                Marshal.ReleaseComObject(object o);
                o = null;
                GC.Collect();
            }

        }
    }
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Delphi - Tfilestream(1)
  2. Force Object To Load Last(2)
  3. VB6 Need Help With Object Validation(4)
  4. Problem With Itunes(5)
  5. Garmon's Javascript #1 - Simple Object Database(1)
  6. Itunes Download Link(0)
  7. iTunes' .m4p Music-files With Bad Quality?(6)
  8. Embedding Object In MySQL(2)
  9. Itunes Music Store Billion Songs Countdown(4)
  10. Itunes And Your MP3 Player(2)
  11. Ruby - Better Programming Language Than Java Or C#?(1)
  12. Euthanasia Or Mercy Killing: Should It Be Legalized?(9)
  13. Rumours Of A Google Music (iTunes Store Competitor)(13)
  14. Object & Classes Trouble(2)
  15. Datasource Object Limitations?(0)
  1. VB 6.0 Ole DB Object/Session Error(1)
  2. Obtain The Audio Output From iTunes(1)
  3. OSCommerce Errors(5)
  4. Linking An Object Tag(0)
  5. Podcasting & Itunes(2)
  6. Php - Object Oriented Programming(1)
  7. C# Tutorial : Lesson 4 - Object Oriented Programming(2)
  8. Problem In Theme Or Active Object!(4)
  9. Itunes Album Art(6)
  10. I Cant Install Itunes, Quicktime, Or Ipod Updater(6)
  11. Itunes - Cannot Connect To Store(1)
  12. Changing Path Of Shared Object(2)
  13. Itunes And Windows Media Player(1)


 



- Lo-Fi Version Time is now: 11th October 2008 - 07:13 AM