Home > Back-end >  API thread error, canvas does not allow drawing, help
API thread error, canvas does not allow drawing, help

Time:09-30


Write a thread function, operating Label1 count in the Form,
DWORD WINAPI ThreadFunc (LPVOID lpParameter)
{
int i;
String s;

for(i=0; I<=100000; I++)
{
S=IntToStr (I);
Form1 - & gt; Label1 - & gt; Caption=s;
}
}
Create a button to start the thread
Void __fastcall TForm1: : Button1Click (TObject * Sender)
{

DWORD threadID;
HHandle=(int) CreateThread (NULL,
0,
ThreadFunc,
(void *) this,
0,
& ThreadID);

}
Click on the button, an error operation, "canvas does not allow drawing", his study for a long time without results, looked at great god help me ~

CodePudding user response:

Preliminary opinion, is the VCL thread operation VCL objects,

According to the code you see, there is no use TThread object, but directly use the API to create threads,

This time, we should first define a custom message, and then, in the thread, SendMessage (custom message, I) (pseudocode)

Then, in the Form1 processing custom message, display messages I bring value to Label1 - & gt; Caption

CodePudding user response:

If you use TThread object, the method is to use the Synchronize can

//Important: the Methods and the properties of objects in the VCL can only be
//2 in a method called using the Synchronize, for example:
//
//the Synchronize (UpdateCaption);
//
//the where UpdateCaption could look like:
//
//void __fastcall asasdf: : UpdateCaption ()
//{
//Form1 - & gt; Caption="Updated in a, thread".
//}