Jump to content
xisto Community
vizskywalker

Cleanly Killing The Itunes Com Object

Recommended Posts

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

Share this post


Link to post
Share on other sites

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:

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

Share this post


Link to post
Share on other sites

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

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...

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.