Click here to Skip to main content
15,399,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is a function to draw shadow,but each shadow takes 2ms to draw,if I create 10 shadow it takes nearly 20ms to draw and make the UI lag.
CreateShadowBox(hWnd, hdc, rc.left + 20, gTopSize, 100,100,Box1, RGB(60,60,60), RGB(10,10,10));
   CreateShadowBox(hWnd, hdc, rc.left + 330, gTopSize, 100,100, Box2, RGB(60,60,60), RGB(10,10,10));

Because the amount of code is too large, I only show the structure:
C++
typedef void (DRAWPANEL)(HWND, HDC);
void CreateBoxShadow(HWND h,HDC hdc,int x, int y, int sizex, int sizey, DRAWPANEL Drawing,COLORREF bcl, COLORREF cl)

The calculation delay can be understood as sleep(2);

What I have tried:

I try to use multithreading to do the calculation at the same time,but I don't know how to achieve it.
DWORD WINAPI VUIShadowBoxThread(LPVOID pf)
{
	PanelHandleFun* p = (PanelHandleFun*)pf;
	CreateBoxShadow(p->h, p->hdc, p->x, p->y, p->cx, p->cy, p->exDraw, p->c1, p->c2);

	return 0;
}
void CreateBoxShadow_T(HWND h, HDC hdc, int x, int y, int sizex, int sizey, DRAWPANEL Drawing, COLORREF bcl, COLORREF cl)
{
	PanelHandleFun p;
	p.h = h;
	p.hdc = hdc;
	p.exDraw = Drawing;
	p.x = x;
	p.y = y;
	p.cx = sizex;
	p.cy = sizey;
	p.c1 = bcl;
	p.c2 = cl;
	HANDLE thread = CreateThread(NULL, NULL, VUIShadowBoxThread, &p, 0, 0);

	if (thread)
	{
		WaitForSingleObject(thread, INFINITE);
		CloseHandle(thread);

	}
}

Does anyone have an idea?Thank you!
Posted
Updated 20-Nov-21 14:31pm
Comments
Rick York 20-Nov-21 22:16pm
   
I think using threads for this is pointless overkill.

Threading is a good idea, but it also creates overhead, so you should avoid the so called thread explosion where a lot of threads are waiting for execution. It is up to you to test how many threads are useful. Better is to have some stabile threads which have an input and output queue or some signalling like mutexes.

But really important in graphics in double buffering or offscreen rendering in which the draw code only is executed to an internal buffer and at last step that buffer is displayed. Here you find a simple Double buffering article which demonstrates the concept. I would also store the offscreen bitmap to increase performance
   
Comments
EnderMo233 20-Nov-21 4:57am
   
I have already using MemDC,but how can I save drawing cache into bitmap,and when I refresh the window it use BitBlt to show the bitmap instead of drawing again?
Of course, that doesn't make much sense.
C++
HANDLE thread = CreateThread(NULL, NULL, VUIShadowBoxThread, &p, 0, 0);
if (thread)	{
  WaitForSingleObject(thread, INFINITE);
  CloseHandle(thread);
}
It makes no sense to start a thread and then wait for it to finish.
It would make more sense to break the task down into several parts and let each thread do one part.
If you are not sure that you do not have to write more than one on the same area, you would still have to secure critical areas with mutex.

If you use double buffering, the final bitmap could be displayed at the end.
It could also make sense to set the thread and base priority high.

As KarstenK has already suggested, it would probably be good to set up the threads only once and to stop them via semaphore when there is nothing more to do.
The threads could then fetch the jobs themselves from a list. The number of treads cannot sensibly be increased arbitrarily.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900