C# 事件未被处理
我正在通过编写一个使用 iTunes COM API 的应用程序来学习 C# 事件处理。我有一个方法应该在 iTunes 停止播放歌曲时运行,但是当我通过点击“停止/暂停”按钮触发应用程序中的事件时,该方法永远不会被调用。
编辑:根据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");
//...
}
更新了 Pastebin 中的源代码此处(第 59-68 行是相关行)。
规格:我的应用程序应该从头到尾播放 Genius 推荐播放列表中的歌曲(iTunes 默认情况下不会连续播放 Genius 推荐)。 StopEvent 应触发列表中的下一首歌曲播放。
I'm learning C# event handling by writing an app that uses the iTunes COM API. I have a method that should run when iTunes stops playing a song, but the method is never getting called when I trigger the event in the app by hitting the "stop/pause" button.
EDIT: Based on dboarman's reply, I deleted the while loop. Now the event does get handled, but only if I restart iTunes prior to running PlayPlaylist(). If I run PlayPlaylist() a second time, the stop event no longer gets fired/handled.
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");
//...
}
Updated source in Pastebin here (lines 59-68 are the relevant ones).
Spec: My app is supposed to play the songs in a Genius recommendations playlist from first to last (iTunes by default doesn't play Genius recommendations consecutively). The StopEvent should trigger the next song in the list to play.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是有问题的完整代码:
我怀疑
while
循环导致事件永远不会触发,因为线程会休眠一秒钟,并且因为true
是,嗯......总是正确的。我会将您的播放列表放入列表中。比如:
看看这对你有什么作用。
Here is the complete code that is in question:
I suspect that the
while
loop is causing the event to never fire because the thread will sleep for a second and becausetrue
is, well...always true.I would put your playlist in into a list. Something like:
See how that works for you.
当您的 itapp 超出范围时,请务必将其发布,
否则您必须重新启动 iTunes 才能再次工作。使用 -= 取消注册事件处理程序可能也不会造成伤害。
When your itapp goes out of scope, be sure to release it with
or you'll have to restart iTunes for it to work again. Unregistering the event handlers with -= probably wouldn't hurt either.
如果您希望线程阻塞并等待事件,您可以使用 ManualResetEvent 类。
在 itapp_OnPlayerStopEvent() 内部,您必须调用:
_resetEvent.Set();
为了回答您原来的问题,我很确定 while 循环没有给线程任何时间来响应停止事件,因此您没有看到它被处理。
If you want the thread to block and wait for the event you can use the ManualResetEvent class.
Inside itapp_OnPlayerStopEvent() you must call:
_resetEvent.Set();
To answer your original question, I'm pretty sure the while loop is not giving the thread any time to respond to the stop event, hence you are not seeing it being handled.
我想知道事件处理程序未取消挂钩是否会导致某个地方出现问题(即 iTunes 保留对应用程序初始实例的单一引用)。这可以解决它吗?我没有 iTunes API,所以我有点盲目;如果浪费时间,请道歉。
I'm wondering if the fact that the event handler doesn't unhook is causing an issue somewhere along the line (i.e. iTunes holds a singular reference to the initial instance of your app). This may solve it? I don't have the iTunes API so I'm flying a little blind; apologize if it's a waste of time.