vizskywalker 0 Report post Posted October 16, 2007 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
vizskywalker 0 Report post Posted October 17, 2007 So, I managed to stumble across the answer just now. The steps are:1) Use Marshall to release the object2) Null the reference3) Run the Garbage Collector to ensure the reference is goneThe 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
ScepterDonFetti 0 Report post Posted December 30, 2007 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