I've got a WritableBitmap that updates on a separate thread, specifically in response to an event.
我有一个WritableBitmap,它在一个单独的线程上更新,特别是响应一个事件。
myWritableBitmap.Lock();
CopyMemory(myWritableBitmap.BackBuffer, ...);
myWritableBitmap.AddDirtyRect(...);
myWritableBitmap.Unlock();
When run on a separate thread as-is, the Lock()
command throws a System.InvalidOperationException
.
当按原样在单独的线程上运行时,Lock()命令会抛出System.InvalidOperationException。
If I run the same code like this:
如果我像这样运行相同的代码:
this.Dispatcher.Invoke(new VoidDelegate(delegate
{
//Same code as above...
}));
No exceptions are thrown and the code runs perfectly. Wouldn't the purpose of the Lock() be to allow multi-threaded access? Any idea why this is happening?
没有抛出异常,代码运行完美。 Lock()的目的不是允许多线程访问吗?知道为什么会这样吗?
1 个解决方案
#1
Many of the WPF drawing functions must run in a STAThread. The second piece of code makes your drawing functions run in the main UI thread.
许多WPF绘图函数必须在STAThread中运行。第二段代码使您的绘图函数在主UI线程中运行。
Edit: A little bit more about threading in WPF found here.
编辑:这里有关于WPF中的线程的更多信息。
#1
Many of the WPF drawing functions must run in a STAThread. The second piece of code makes your drawing functions run in the main UI thread.
许多WPF绘图函数必须在STAThread中运行。第二段代码使您的绘图函数在主UI线程中运行。
Edit: A little bit more about threading in WPF found here.
编辑:这里有关于WPF中的线程的更多信息。