代码如下:  
procedure  test;  
begin  
 showmessage('OK');  
end;  
 
procedure  TForm1.Button1Click(Sender:  TObject);  
var  
 hthread:thandle;  
 thid:dword;  
begin  
 hthread:=beginthread(nil,0,@test,nil,0,thid);  
 if  hthread=0  then  
   showmessage('Failed');  
end;  
但在点button1的时候报错说project2.exe  raised  exception  class  EInvalidOperation  with  message  'Canvas  does  not  allow  drawing'.  Process  stopped.Use  step  or  Run  to  coninue。  
请问这是什么原因呢?  
而且好像我每次重新打开这个工程并运行的时候说可以的,但第二次又不行了,请问该如何解决呢?谢谢  

解决方案 »

  1.   

    不知道怎么无法现实完全  
    再发一次  
     
    代码如下:      
    procedure    test;      
    begin      
     showmessage('OK');      
    end;      
       
    procedure    TForm1.Button1Click(Sender:    TObject);      
    var      
     hthread:thandle;      
     thid:dword;      
    begin      
     hthread:=beginthread(nil,0,@test,nil,0,thid);      
     if    hthread=0    then      
         showmessage('Failed');      
    end;      
    但在点button1的时候报错说project2.exe    raised    exception    class    EInvalidOperation    with    message    'Canvas    does    not    allow    drawing'.    Process    stopped.Use    step    or    Run    to    coninue。      
    请问这是什么原因呢?      
    而且好像我每次重新打开这个工程并运行的时候说可以的,但第二次又不行了,请问该如何解决呢?谢谢      
      

  2.   

    HThread : Handle ;
    ThreadID : DWord ;
    HThread:=CreateThread(nil,0,@ThreadFuncName,nil,0,ThreadID);ThreadFuncName : 线程函数
      

  3.   

    你的问题是由于线程与VCL组件发生了并行处理,造成了重入问题。VCL在主线程中运行,你的线程必须使用Synchronize去同步它,否则,嘿嘿.....
      

  4.   

    你将test过程的
    showmessage('OK');
    换成
    MessageBox(Application.Handle,'Test OK','OK',MB_OK);
    看看,如何!
    是不是可以了呢。
    这是因为MessageBox是API函数,不是VCL函数,就不会引起重入了。
    当然问题解决了。