как рисовать из одного потока в другом?
Есть собственный таймер, созданный в другом потоке.
Timer::Timer(const std::chrono::milliseconds& interval,
const std::function<void()>& task,
bool singleShot)
: mInterval(interval),
mTask(task),
mSingleShot(singleShot),
mRunning(false)
{
}
void Timer::Start()
{
Stop();
mRunning = true;
mThread = std::thread([this]
{
while (true)
{
std::unique_lock<std::mutex> lock(mRunCondMutex);
auto waitResult = mRunCondition.wait_for(lock, mInterval, [this] { return !mRunning; });
if (mRunning && !waitResult)
mTask();
if (mSingleShot)
mRunning = false;
}
});
}
в GIF.cpp я создаю воспроизведение гиф анимации
GIF::GIF(LPCTSTR sFileName, HDC hdc)
{
Graphics graphics(hdc);
CurrentFrame = 0;
gifBitmap = new Bitmap(sFileName);
UINT count = gifBitmap->GetFrameDimensionsCount();
GUID* pDimensionIDs = (GUID*)malloc(sizeof(GUID) * count);
gifBitmap->GetFrameDimensionsList(pDimensionIDs, count);
frameCount = gifBitmap->GetFrameCount(&pDimensionIDs[0]);
//frameAray = (UINT*)malloc(frameCount);
/*установить в frameAray DrawCachedBitmap, в деструкторе delete()*/
free(pDimensionIDs);
//std::cout<<"ff " << _msize(frameAray);
}
void GIF::draw(HDC g, UINT CurrentFrame)
{
Graphics graphics(g);
GUID guid = FrameDimensionTime;
gifBitmap->SelectActiveFrame(&guid, CurrentFrame);
/*выводить кадры*/
//gifBitmap->UnlockBits(&bitmapData);
this->position.x = 50;
this->position.y = 50;
::std::shared_ptr<::CachedBitmap> cbitmap = PNGLoader::createCachedBitmap(gifBitmap, g);
graphics.DrawCachedBitmap(cbitmap.get(), position.x, position.y);
++CurrentFrame;
if (CurrentFrame > frameCount) CurrentFrame = 0;
std::cout << CurrentFrame << std::endl;
//std::shared_ptr
}
void GIF::Play(Graphics* g, HDC hdc)
{
Timer *mTimer = new Timer(std::chrono::milliseconds(100), [&]
{
++CurrentFrame;
this->draw(hdc, CurrentFrame);
std::cout << hdc;
}, false);
mTimer->Start();
}
main.cpp:
splash = new GIF(L"resoureses/Sample.gif", hdc);
splash->draw(hdc, 20);
splash->Play(gt,hdc);
Но при рисовании в другом потоке, как я понимаю теряется hdc, если рисовать в одном потоке, то все хорошо. Как мне рисовать из одного потока в другом? Или можно вообще придумать альтернативу?