从工人线程打印,怎么样? [英] Printing from a worker thread, how?

查看:90
本文介绍了从工人线程打印,怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是如此计算限制,它可以运行大约一分钟。在此期间,它正在绘制到屏幕上,但你无法阻止它。我猜,消息泵已经停止了。我已经解决了绘画问题而不是印刷问题,所以我会给你绘画的要点。我在OnPaint中启动工作线程并将线程parms传递给this指针和HWND hwnd。 hwnd来自GetSafeHwnd()。 OnPaint现在已经完成并返回。



我已经学会了不使用CDC在工人(画家)线程中,我这样做:



My app is so compute bound that it can run for on the order of a minute. During that time, it is drawing to the screen, but you cannot stop it. The message pumps have stopped, I presume. I have solved the painting problem but not the printing problem, so I'll give you the gist of painting. I start the worker thread in OnPaint and pass the thread parms the "this" pointer and HWND hwnd. The hwnd comes from GetSafeHwnd(). OnPaint is now done and returns.

I've learned not to use CDC In the worker (painter) thread, I do this:

HDC hdc = GetDC(ps->hwnd);   // ps = pointer to thread parm structure
DrawAll(hdc);     // heavy duty computing here
ReleaseDC(ps->hwnd);
return;



OnPrint()类似于OnPaint()但有点复杂。起始线程如下所示:




OnPrint() is similar to OnPaint() but is a tad more complicated. The starter thread looks like this:

void CMyView::StartPrinterThread(CDC* pdc, CPrintInfo* pInfo)
{
    PrintParam.pv = (CMyView*)this;
    CMyDoc* pDoc = GetDocument();
    PrintParam.pInfo = pInfo;

    pDoc->m_bStopThread = FALSE; // so can stop with a mouse click
    ResetEvent(pDoc->m_hevPrinter);
    AfxBeginThread(PrinterThread,&PrintParam);
    WaitForSingleObject(pDoc->m_hevPrinter, INFINITE);
}

static UINT PrinterThread(LPVOID pParam)
{
    CPrintInfo* pInfo;
    HDC hdc;
    RECT rcl;
    POINT Org;
    PRINTPARMSTRUCT* ps = (PRINTPARMSTRUCT*)pParam;
    // some app specific stuff
    CMyDoc* pDoc = ps->pv->GetDocument();
    pInfo = ps->pInfo;
    hdc = pInfo->m_pPD->m_pd.hDC;
    SetMapMode(hdc, MM_LOENGLISH);
    rcl = pInfo->m_rectDraw;
    ...
    SetEvent(pDoc->m_hevPrinter);  // starter thread going
    DrawAll(hdc);     // heavy duty computing here
    return 0;
}





我的应用程序实际上按预期运行但是有一个小细节。打印预览页面和打印页面为空白。我希望这是我想念的小事。



Tom Stokes



My app actually runs as expected but for a small detail. The print preview page and the printed page are blank. I hope it is something small that I am missing.

Tom Stokes

推荐答案

你可能会喜欢看看 http://msdn.microsoft.com/en-us/library/3dy7kd92.aspx [ ^ ]了解有关处理计算限制情况的最佳方法。
You may like to look at http://msdn.microsoft.com/en-us/library/3dy7kd92.aspx[^] for a description of the best way to handle compute bound situations.


这篇关于从工人线程打印,怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆