By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,805 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,805 IT Pros & Developers. It's quick & easy.

When closing iTunes or my app, errors occur

P: 1
What I want to do is get song info from iTunes if the application is running, using the ituneslib API (from Apple).

One of the problem is that to check if iTunes is playing any songs I need to make the COM object right?
But when I make it, it starts up the iTunes.exe application if iTunes is not already running.
To counter that I made checkiTunesAlive, wich goes trough the running processes in windows and returns true if iTunes.exe is running.

The other problems are that if I close my program while iTunes is running, iTunes goes on fine until I close the iTunes app, a error telling me an unhandled exception happened in iTunes.exe. When discarding the error iTunes shuts down and then starts up again, shutting it down for the secound time goes without problems

If I close iTunes first while both programs are running, iTunes reports an error, when i go past the error iTunes shuts down and starts back up again, this is becouse my iTunes process check still reports iTunes.exe in the process list while its shutting down, and when iTunes finally closes, my code is already past the iTunes.exe process list check, and making the new iTunes COM object to check for what songs its playing. How do I solve this problem?

Some times everything works OK when shutting down my app or iTunes when both are running, but that only happens about 10% of the times I try.

When both applications are running everything works as expected!

Please advice.
Expand|Select|Wrap|Line Numbers
  1.         private string SongItunes()
  2.         {
  3.             if (checkiTunesAlive() > 0)
  4.             {
  5.                 string song = string.Empty;
  6.  
  7.                 try
  8.                 {
  9.                     iTunesLib.IiTunes iTunesApp;
  10.                     iTunesApp = new iTunesLib.iTunesAppClass();
  11.  
  12.                     if (iTunesApp.CurrentTrack.Name != null)
  13.                     {
  14.                         song += iTunesApp.CurrentTrack.Artist;
  15.  
  16.                         if (iTunesApp.CurrentTrack.Artist != null)
  17.                         {
  18.                             song += " - ";
  19.                             song += iTunesApp.CurrentTrack.Name;
  20.                         }
  21.  
  22.                     }
  23.  
  24.                     //Release COM object
  25.                     Marshal.ReleaseComObject(iTunesApp.CurrentTrack);
  26.                     Marshal.ReleaseComObject(iTunesApp);
  27.                     iTunesApp = null;
  28.                 }
  29.                 catch (Exception e)
  30.                 {
  31.                     messageSend("ERROR!", e.Message);
  32.                 }
  33.  
  34.                 return song;
  35.             }
  36.             return "N/A";
  37.         }
  38.  
  39.         private int checkiTunesAlive()
  40.         {
  41.              Process[] proc = Process.GetProcessesByName("iTunes");
  42.              return proc.Length;
  43.         }
  44.  
  45.  
Sep 16 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.