出现不可预料的出错的ShowMessage of AnsiString; - C++ Builder / Windows SDK/API
ShowMessage的问题,在线程中调用,出现字串没有显示,或是很长的现象,随机出现的。
代码如下,
DWORD WINAPI ThreadFunc( LPVOID p)
{ DWORD dwSigned;
AnsiString str;
int i;
i=0;
for( i=1;i<5;i++)
{
dwSigned= WaitForSingleObject( hSign,INFINITE );
str.sprintf("dwSigned:%d i:%d",dwSigned,i);
ShowMessage(str);
}
return 0;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
hthread=CreateThread(NULL,
0,
ThreadFunc,
this,
CREATE_SUSPENDED,
&threadid );
//LPCTSTR lpName ;
AnsiString str;
hSign=CreateEvent(NULL,false,true,"dr"); //
str.sprintf("hthread:%d ,threadid:%d",hthread,threadid);
ShowMessage(str);
ResumeThread(hthread);
// CloseHandle(threadid);
}
void __fastcall TForm1::Button4Click(TObject *Sender)
{
SetEvent(hSign);
}
//---------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
ResetEvent(hSign);
}
[解决办法]
这个要用同步方法的
[解决办法]
不要在线程函数中ShowMessage等界面相关操作,应该发送消息到主界面,然后主界面响应消息显示。
[解决办法]