代码之家  ›  专栏  ›  技术社区  ›  RexE

C#未处理事件

  •  3
  • RexE  · 技术社区  · 16 年前

    编辑 :根据dboarman的回复,我删除了while循环。现在事件确实得到了处理,但前提是我在运行PlayPlaylist()之前重新启动iTunes。如果再次运行PlayPlaylist(),停止事件将不再触发/处理。

    void trayIcon_Click(object sender, EventArgs e)
    {
        PlayPlaylist();
    }
    
    public static void PlayPlaylist()
    {
    
        itapp = new iTunesApp();
        itapp.OnPlayerStopEvent += 
            new _IiTunesEvents_OnPlayerStopEventEventHandler(itapp_OnPlayerStopEvent);
    
        lastPlaylist = itapp.LibraryPlaylist;
    
        itapp.Play();            
    }
    
    static void itapp_OnPlayerStopEvent(object iTrack)
    {
        Debug.WriteLine("Stop Event fired");
        //...
    }
    

    here (第59-68行是相关的)。

    4 回复  |  直到 16 年前
        1
  •  1
  •   JustABill    16 年前

    当您的itapp超出范围时,请确保使用

    System.Runtime.InteropServices.Marshal.ReleaseComObject(itapp);
    

    否则你必须重新启动iTunes,它才能再次工作。用-=注销事件处理程序可能也不会有什么坏处。

        2
  •  2
  •   IAbstract    16 年前

    这是你的名字 完成 有问题的代码:

    public static void PlayPlaylist()
    {
        itapp = new iTunesApp();
        itapp.OnPlayerStopEvent += new _IiTunesEvents_OnPlayerStopEventEventHandler(itapp_OnPlayerStopEvent);
    
        lastPlaylistID = itapp.LibraryPlaylist.playlistID;
        Debug.WriteLine("Last playlist:");
        Debug.WriteLine(lastPlaylistID);
    
        itapp.Play();
    
        while (true)
        {
            System.Threading.Thread.Sleep(1000);
        }
    }
    

    while 循环导致事件从不触发,因为线程将休眠一秒钟,并且 true 永远都是真的。

    我会把你的播放列表放到一个列表里。比如:

    static List<myTrack> Tracks;
    
    public static void PlayPlaylist() 
    {
        itapp = new iTunesApp();
        itapp.OnPlayerStopEvent += new _IiTunesEvents_OnPlayerStopEventEventHandler(itapp_OnPlayerStopEvent);
    
        foreach (myTrack track in Tracks)
        {
        // perform play
        }
    }
    

    看看你是怎么做的。

        3
  •  1
  •   Phillip Ngan    16 年前

    如果希望线程阻塞并等待事件,可以使用ManualResetEvent类。

    private ManualResetEvent _resetEvent;
    
    public void PlayPlaylist() 
    {
        _resetEvent = new ManualResetEvent(false);
        itapp = new iTunesApp();
        itapp.OnPlayerStopEvent += new _IiTunesEvents_OnPlayerStopEventEventHandler(itapp_OnPlayerStopEvent);
    
    
        // Block until the resetEvent has been Set() or
        // give up waiting after 5 minutes
        _resetEvent.WaitOne(1000*5*60); 
    }
    

    _resetEvent.Set();

    为了回答您最初的问题,我非常确定while循环没有给线程任何时间来响应stop事件,因此您没有看到它被处理。

        4
  •  1
  •   Jesse C. Slicer    13 年前

    也许 解决它?我没有iTunesAPI,所以我有点盲目;如果这是浪费时间,请道歉。

    private bool stopIt;
    
    private void trayIcon_Click(object sender, EventArgs e)
    {
        if (!stopIt)
        {
            PlayPlaylist();
            stopIt = true;
        }
        else
        {
            StopPlaylist();
            stopIt = false;
        }
    }
    
    public static void PlayPlaylist()
    {
    
        itapp = new iTunesApp();
        itapp.OnPlayerStopEvent += 
            new _IiTunesEvents_OnPlayerStopEventEventHandler(itapp_OnPlayerStopEvent);
    
        lastPlaylist = itapp.LibraryPlaylist;
    
        itapp.Play();            
    }
    
    public static void StopPlaylist()
    {
        itapp.Stop(); // don't know if this is the right method name
    
        // unhook the event so the reference object can free if necessary.
        itapp.OnPlayerStopEvent -= 
            new _IiTunesEvents_OnPlayerStopEventEventHandler(itapp_OnPlayerStopEvent);
    }
    
    private static void itapp_OnPlayerStopEvent(object iTrack)
    {
        Debug.WriteLine("Stop Event fired");
        // ...
    }
    
    推荐文章