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
- private string SongItunes()
- {
- if (checkiTunesAlive() > 0)
- {
- string song = string.Empty;
- try
- {
- iTunesLib.IiTunes iTunesApp;
- iTunesApp = new iTunesLib.iTunesAppClass();
- if (iTunesApp.CurrentTrack.Name != null)
- {
- song += iTunesApp.CurrentTrack.Artist;
- if (iTunesApp.CurrentTrack.Artist != null)
- {
- song += " - ";
- song += iTunesApp.CurrentTrack.Name;
- }
- }
- //Release COM object
- Marshal.ReleaseComObject(iTunesApp.CurrentTrack);
- Marshal.ReleaseComObject(iTunesApp);
- iTunesApp = null;
- }
- catch (Exception e)
- {
- messageSend("ERROR!", e.Message);
- }
- return song;
- }
- return "N/A";
- }
- private int checkiTunesAlive()
- {
- Process[] proc = Process.GetProcessesByName("iTunes");
- return proc.Length;
- }