I think I found the error. Direct3DHook uses a threadpool to run the code above.
ThreadPool.QueueUserWorkItem(delegate
{
using (MemoryStream ms = new MemoryStream())
{
Texture2D.ToStream(textureDest.Device.ImmediateContext, textureDest, ImageFileFormat.Bmp, ms);
SendResponse(ms, requestId);
this.DebugMessage("PresentHook: Send response time: " + (DateTime.Now - startSendResponse).ToString());
}
// Free the textureDest as we no longer need it.
textureDest.Dispose();
textureDest = null;
});
But the Texture.ToStream call needs to be single threaded because internally it probably issues the GetRenderTarget call which is single threaded and will cause an exception if two calls are started asynchronously. I removed the ThreadPool call and it worked okay.