A ansili Unregistered / Unconfirmed GUEST, unregistred user! 2005-11-22 #1 那位在多线程里用过事件对象(TEvent)? 当2个线程操作一个队列时,一个写一个读。我想同步。 麻烦给个完整示例。
A ansili Unregistered / Unconfirmed GUEST, unregistred user! 2005-11-22 #2 那位在多线程里用过事件对象(TEvent)? 当2个线程操作一个队列时,一个写一个读。我想同步。 麻烦给个完整示例。
P pascal Unregistered / Unconfirmed GUEST, unregistred user! 2005-12-12 #3 void __fastcall TForm1::FormCreate(TObject *Sender) { InitComm(1); SyncEvent = CreateEvent(NULL, true, true, "Comm_Sync"//先要做个Event for (int i=0; i<15; i++) { Frame = new TMutilFrame(this); Frame->Parent = this; Frame->Name = "Frame" + IntToStr(i); } } void __fastcall TMutilFrame::BtnOpenClick(TObject *Sender) //这个是按键的,要防和定时器冲突 { if (WaitForSingleObject(SyncEvent,200) == WAIT_OBJECT_0) //等允许Event { //等到 ResetEvent(Form1->SyncEvent); //开始同步,不允许其他线程打断 do Something;//不能和定时器同时做doSomething SetEvent(Form1->SyncEvent);//完成,定时器可以工作(如果要工作的话); } else { //等不到Event,200ms超时 do Nothing; } } void __fastcall TMutilFrame::Timer1Timer(TObject *Sender)//这个是定时器的,要防和按键冲突 { if (WaitForSingleObject(Form1->SyncEvent,200) == WAIT_OBJECT_0) { ResetEvent(Form1->SyncEvent); do SomeThing; SetEvent(Form1->SyncEvent); } } }
void __fastcall TForm1::FormCreate(TObject *Sender) { InitComm(1); SyncEvent = CreateEvent(NULL, true, true, "Comm_Sync"//先要做个Event for (int i=0; i<15; i++) { Frame = new TMutilFrame(this); Frame->Parent = this; Frame->Name = "Frame" + IntToStr(i); } } void __fastcall TMutilFrame::BtnOpenClick(TObject *Sender) //这个是按键的,要防和定时器冲突 { if (WaitForSingleObject(SyncEvent,200) == WAIT_OBJECT_0) //等允许Event { //等到 ResetEvent(Form1->SyncEvent); //开始同步,不允许其他线程打断 do Something;//不能和定时器同时做doSomething SetEvent(Form1->SyncEvent);//完成,定时器可以工作(如果要工作的话); } else { //等不到Event,200ms超时 do Nothing; } } void __fastcall TMutilFrame::Timer1Timer(TObject *Sender)//这个是定时器的,要防和按键冲突 { if (WaitForSingleObject(Form1->SyncEvent,200) == WAIT_OBJECT_0) { ResetEvent(Form1->SyncEvent); do SomeThing; SetEvent(Form1->SyncEvent); } } }