当 AxWindowsMediaPlayer 关闭时,出现 AccessViolation 异常

发布于 2024-07-15 18:09:39 字数 196 浏览 13 评论 0原文

我的表单上有一个 AxWMPLib.AxWindowsMediaPlayer 。 当我关闭表单时,我收到“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。” 例外。 隐藏表单可以,但关闭则不行。 当组件从表单中删除时,一切都很好。

这是Winforms .Net3.5。

任何帮助表示赞赏。

I have a AxWMPLib.AxWindowsMediaPlayer on a form. When I close the form, I get "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." exception. It is OK with hiding the form but not with closing. Everything's fine when the component is removed from the form.

This is Winforms .Net3.5.

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔一刀 2024-07-22 18:09:39

这发生在我身上,而且是在按键期间关闭表单时。

如果 WMP 控件有一个关键事件需要处理,那么它似乎会引起问题。

Form.KeyPreview = True 的示例

Sub Form_KeyDown(e As KeyEventArgs)
 AxWindowsMediaPlayer1.Dispose()
End Sub

会导致访问冲突。

Sub Form_KeyDown(e As KeyEventArgs)
 e.Handled = True
 AxWindowsMediaPlayer1.Dispose()
End Sub

完全关闭,因为按键被阻止到达 WMP 控件。

当窗体关闭时会发生同样的事情,因为将释放控件。

This was happening to me, and it was when closing the form during a key press.

Seems the WMP control will cause problems if it has a key event to process.

Example with Form.KeyPreview = True

Sub Form_KeyDown(e As KeyEventArgs)
 AxWindowsMediaPlayer1.Dispose()
End Sub

Causes an Access Violation.

Sub Form_KeyDown(e As KeyEventArgs)
 e.Handled = True
 AxWindowsMediaPlayer1.Dispose()
End Sub

Closes cleanly, as the key press is blocked from reaching the WMP control.

Same thing happens when the form is closed as will dispose of the control.

病毒体 2024-07-22 18:09:39

有时,在 .NET 应用程序中使用 ActiveX 对象时,有必要在退出时强制进行垃圾回收。 我通常在 Form_Closing 中使用以下方法执行此操作:

GC.WaitForPendingFinalizers()
GC.Collect()

另外,如果您为该对象设置了任何事件处理程序,您将需要显式断开它们的连接。 我多次发现 ActiveX 对象在垃圾箱中仍然保持活动状态,并且即使在它们被处理后也会尝试调用事件处理程序。

在尝试处理该对象之前,确保播放已停止也可能是值得的。

Sometimes when working with ActiveX objects in .NET applications it is necessary to force garbage collection on exit. I generally do this in Form_Closing using:

GC.WaitForPendingFinalizers()
GC.Collect()

Also, if you have setup any event handlers for the object, you will want to disconnect them explicitly. I've found on a number of occasions that ActiveX objects will still remain active in the garbage bin and will attempt to call the event handler even after they have been disposed.

It may also be worth it to make sure playback has stopped before you try to dispose of the object.

墨落画卷 2024-07-22 18:09:39

ActiveX 对象可能对在父窗体关闭时以正确的顺序关闭有一些敏感的依赖关系,否则它们可能会继续存在,直到 gc 运行 - 尝试在控件的界面中查找看起来它们可能必须执行的任何方法关闭或销毁对象并调用它们。

ActiveX objects may have some sensitive dependencies on being closed in the correct order when the parent form is closed, otherwise they may go on living until gc runs - try looking through the interface for the control for any methods that look like they may have to do with closing, or destroying the object and calling those.

药祭#氼 2024-07-22 18:09:39

我想我已经有了:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    axWindowsMediaPlayer1.close();
}

只是在文档 http://msdn.microsoft.com/en-us/library/windows/desktop/dd562388(v=vs.85).aspx
我想我应该尝试一下。 现在看起来好多了。

I think I have it:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    axWindowsMediaPlayer1.close();
}

simply found the method on the doc http://msdn.microsoft.com/en-us/library/windows/desktop/dd562388(v=vs.85).aspx
I thought I'd give it a go. it seems much better now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文