Cleanly Killing The Itunes Com Object

free web hosting
Free Web Hosting > Computers & Tech > Programming > Programming General > C# .NET

Cleanly Killing The Itunes Com Object

vizskywalker
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

Reply

vizskywalker
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

Reply

ScepterDonFetti
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();
            }

        }
    }

 

 

 


Reply


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Similar Topics

Keywords : cleanly killing itunes

  1. Obtain The Audio Output From iTunes - (1)



Looking for cleanly, killing, itunes, object

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for cleanly, killing, itunes, object
advertisement




Cleanly Killing The Itunes Com Object



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE