|
|
|
|
![]() ![]() |
Oct 16 2007, 05:31 AM
Post
#1
|
|
|
Techno-Necromancer 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 |
|
|
|
Oct 17 2007, 05:45 PM
Post
#2
|
|
|
Techno-Necromancer 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 |
|
|
|
Dec 30 2007, 07:56 AM
Post
#3
|
|
|
Newbie [ Level 2 ] 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(); } } } |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 11th October 2008 - 07:13 AM |