gpt4 book ai didi

c# - 如何将 DispatcherObject (BitmapSource) 复制到不同的线程中?

转载 作者:太空狗 更新时间:2023-10-29 22:13:07 25 4
gpt4 key购买 nike

我想知道如何将 DispatcherObject(在我的例子中是 BitmapSource)复制到另一个线程中。

用例:
我有一个 WPF 应用程序需要在新线程中显示窗口(该应用程序实际上是 Outlook 插件,我们需要这样做,因为 Outlook 在主 UI 线程中有一些 Hook 并且正在窃取我们需要使用的某些热键 - '丢失在 Outlook、WPF(我们用于 UI)和 Winforms(我们需要使用某些微软提供的 winforms 控件)的互操作中进行翻译”。

这样,我就有了 WPFMessageBox 的实现,它是通过设置一些静态属性来配置的——其中之一是图标的 BitmapSource。这是为了在启动时我可以设置 WPFMessageBox.Icon 一次,从那时起,每个 WPFMessageBox 都将具有相同的图标。

问题是分配给icon的BitmapSource是一个DispatcherObject,读取时会抛出InvalidOperationException:“调用线程无法访问这个对象,因为另一个线程拥有它。”。

如何将 BitmapSource 克隆到实际线程中?它有 Clone() 和 CloneCurrentValue() 方法,它们不起作用(它们也抛出相同的异常)。我还想到使用 originalIcon.Dispatcher.Invoke(在此处进行克隆) - 但 BitmapSource 的 Dispatcher 为空,而且仍然 - 我会在错误的线程上创建一个副本,但仍然无法在我的线程上使用它。 BitmapSource.IsFrozen == true。

关于如何将 BitmapSource 复制到不同的线程(而不是在新线程中从图像文件完全重建它)有什么想法吗?

编辑:所以,卡住没有帮助:最后我有一个 BitmapFrame(Window.Icon 无论如何都不采用任何其他类型的 ImageSource),当我将它分配为 Window.Icon 在不同的线程上,即使卡住,我get InvalidOperationException:“调用线程无法访问此对象,因为另一个线程拥有它。”具有以下堆栈跟踪:

    WindowsBase.dll!System.Windows.Threading.Dispatcher.VerifyAccess() + 0x4a bytes 
WindowsBase.dll!System.Windows.Threading.DispatcherObject.VerifyAccess() + 0xc bytes
PresentationCore.dll!System.Windows.Media.Imaging.BitmapDecoder.Frames.get() + 0xe bytes
PresentationFramework.dll!MS.Internal.AppModel.IconHelper.GetIconHandlesFromBitmapFrame(object callingObj = {WPFControls.WPFMBox.WpfMessageBoxWindow: header}, System.Windows.Media.Imaging.BitmapFrame bf = {System.Windows.Media.Imaging.BitmapFrameDecode}, ref MS.Win32.NativeMethods.IconHandle largeIconHandle = {MS.Win32.NativeMethods.IconHandle}, ref MS.Win32.NativeMethods.IconHandle smallIconHandle = {MS.Win32.NativeMethods.IconHandle}) + 0x3b bytes
> PresentationFramework.dll!System.Windows.Window.UpdateIcon() + 0x118 bytes
PresentationFramework.dll!System.Windows.Window.SetupInitialState(double requestedTop = NaN, double requestedLeft = NaN, double requestedWidth = 560.0, double requestedHeight = NaN) + 0x8a bytes
PresentationFramework.dll!System.Windows.Window.CreateSourceWindowImpl() + 0x19b bytes
PresentationFramework.dll!System.Windows.Window.SafeCreateWindow() + 0x29 bytes
PresentationFramework.dll!System.Windows.Window.ShowHelper(object booleanBox) + 0x81 bytes
PresentationFramework.dll!System.Windows.Window.Show() + 0x48 bytes
PresentationFramework.dll!System.Windows.Window.ShowDialog() + 0x29f bytes
WPFControls.dll!WPFControls.WPFMBox.WpfMessageBox.ShowDialog(System.Windows.Window owner = {WPFControlsTest.MainWindow}) Line 185 + 0x10 bytes C#

最佳答案

调用Freeze 后,它应该可以在多个线程上运行。

关于c# - 如何将 DispatcherObject (BitmapSource) 复制到不同的线程中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2759069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com