Thread View: borland.public.cppbuilder.nativeapi
5 messages
5 total messages
Started by Phuoc Can HUA
Mon, 07 Apr 2003 00:10
Problem with Thread and OnPaint
Author: Phuoc Can HUA
Date: Mon, 07 Apr 2003 00:10
Date: Mon, 07 Apr 2003 00:10
25 lines
2093 bytes
2093 bytes
I'm using a Canvas (WorkSpace) where I do my drawing and a PaintBox to display the results. A Thread is used to draw on WorkSpace and copy the modified segment into PaintBox. Another procedure, triggered by OnPaint event, copies the entire WorkSpace into the PaintBox to restore the image. Semaphore is used to ensure that the restore procedure will not copy an unfinished image. Canvas are locked while they are in use. Now, when I move the main form over the form with the PaintBox very fast the image can't be restored properly. It looks like Windows can detect the 'invalid' area and only updates a small part of the PaintBox, although my procedure copies the entire image. That's OK. But the 'funny' thing is that the update segment sometimes appears at the wrong place, somtimes even somewhere on the screen (outside of the paintbox). Of course, sometimes it crashes as well :-( The program was working fine without Thread. Can anyone help? Thanks Phuoc -- _/\__/\__/\__/\__/\__/\_ www.huaonline.com My Homepage is my Castle
Re: Problem with Thread and OnPaint
Author: Remy Lebeau (TeamB)
Date: Mon, 07 Apr 2003 04:29
Date: Mon, 07 Apr 2003 04:29
26 lines
2341 bytes
2341 bytes
It is hard to diagnose your problem without seeing yuor actual code. But offhand, it sounds like you are trying to draw directly onto the PaintBox from your thread directly. If so, then you shouldn't be doing that, as the VCL is not thread-safe and can't handle cross-thread painting correctly. Instead, you should be doing your thread-based drawing to a separate in-memory bitmap that is properly protected by a synchronization object (mutex, critical section, semaphore, etc), and then the thread can obtain the lock to the bitmap when it needs to alter the bitmap, and then the OnPaint event can obtain the lock when it needs to update the PaintBox with the bitmap. That way, only one is trying to access the bitmap at any given time. Gambit "Phuoc Can HUA" <home+a+t+huaonline.com> wrote in message news:3e90c1f3$1@newsgroups.borland.com... > Now, when I move the main form over the form with the > PaintBox very fast the image can't be restored properly. It looks > like Windows can detect the 'invalid' area and only updates a > small part of the PaintBox, although my procedure copies the > entire image. That's OK. But the 'funny' thing is that the update > segment sometimes appears at the wrong place, somtimes even > somewhere on the screen (outside of the paintbox). Of course, > sometimes it crashes as well :-(
Re: Problem with Thread and OnPaint
Author: Phuoc Can HUA
Date: Mon, 07 Apr 2003 05:08
Date: Mon, 07 Apr 2003 05:08
36 lines
2669 bytes
2669 bytes
"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message news:3e90fe9b$1@newsgroups.borland.com... > It is hard to diagnose your problem without seeing yuor actual code. But > offhand, it sounds like you are trying to draw directly onto the PaintBox > from your thread directly. If so, then you shouldn't be doing that, as the Yes, the thread is drawing using several Canvas and copy the results from the WorkSpace to PaintBox and this is running all the time. I had some problems with Canvas before, until I use Lock/Unlock. I also tried to use lock on PaintBox. But somehow the whole OnPaint routine stuck :-) > VCL is not thread-safe and can't handle cross-thread painting correctly. > Instead, you should be doing your thread-based drawing to a separate > in-memory bitmap that is properly protected by a synchronization object > (mutex, critical section, semaphore, etc), and then the thread can obtain > the lock to the bitmap when it needs to alter the bitmap, and then the > OnPaint event can obtain the lock when it needs to update the PaintBox with > the bitmap. That way, only one is trying to access the bitmap at any given > time. Haven't got all what you mean. Do you mean... the thread only draws on a bitmap (Canvas?) without copying the results to the PaintBox. But how can I display the results? Do I have to trigger OnPaint to refresh the image? Will the OnPaint re-paint the entire image? Currently, my OnPaint seems to be running 'wild'. As I mentioned, sometimes it just draws on the screen across several windows. Phuoc Can HUA -- _/\__/\__/\__/\__/\__/\_ www.huaonline.com My Homepage is my Castle
Re: Problem with Thread and OnPaint
Author: Remy Lebeau (TeamB)
Date: Mon, 07 Apr 2003 17:35
Date: Mon, 07 Apr 2003 17:35
38 lines
2466 bytes
2466 bytes
"Phuoc Can HUA" <home+a+t+huaonline.com> wrote in message news:3e9107d2$1@newsgroups.borland.com... > Haven't got all what you mean. Do you mean... the thread only > draws on a bitmap (Canvas?) Not a Canvas, no. Well, yes, but not a standalone TCanvas, but an actual TBitmap instead. Or, since you're posting this in the .nativepapi group, a call to CreatecomatibleDC(), CreateCompatibleBitmap(), SelectObject(), and any drawing functions you're using. > without copying the results to the PaintBox. Not from the thread itself, no. > But how can I display the results? Put the bitmap where the PaintBox can get to it, and then it's OnPaint event goes and grabs the current bitmap when needed. > Do I have to trigger OnPaint to refresh the image? Not directly, no. However, your thread could Invalidate() the PaintBox after rendering the bitmap. Invalidate() wil force the PaintBox to redraw itself when it is actually ready to do so, which will then trigger OnPaint indirectly. > Currently, my OnPaint seems to be running 'wild'. As I mentioned, > sometimes it just draws on the screen across several windows. I can't comment on that as you never posted your actual source code. But that sounds like a symptom of logic errors in your own code, or you're at least not clipping the image first before drawing it so it doesn't go out of the boundaries of the area of the PaintBox ou want to draw in only. Gambit
Re: Problem with Thread and OnPaint
Author: Phuoc Can HUA
Date: Tue, 08 Apr 2003 00:03
Date: Tue, 08 Apr 2003 00:03
17 lines
1910 bytes
1910 bytes
"Remy Lebeau (TeamB)" <gambit47@yahoo.com> wrote in message news:3e91b72c$3@newsgroups.borland.com... > Not a Canvas, no. Well, yes, but not a standalone TCanvas, but an actual > TBitmap instead. Or, since you're posting this in the .nativepapi group, a > call to CreatecomatibleDC(), CreateCompatibleBitmap(), SelectObject(), and > any drawing functions you're using. I was thinking to post it in the graphic group. But I thought this is much more a problem with the thread. Thanks for your alternative solution. I'm going to try it. > I can't comment on that as you never posted your actual source code. But > that sounds like a symptom of logic errors in your own code, or you're at Not that I want to keep my codes secret. I don't have my codes here (in the office). Next time I will make sure I've got the latest copy here. Phuoc Can HUA
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads