delphi中基于事件通知的多线程应用计算机二级考试
文章作者 100test 发表时间 2009:05:22 12:02:13
来源 100Test.Com百考试题网
2009年下半年全国计算机二级考试你准备好了没?考计算机等级考试的朋友,2009年下半年全国计算机二级考试时间是2009年9月19日至23日。更多优质资料尽在百考试题论坛 百考试题在线题库
以前写过写多线程,往往是一个死循环中采用sleep来把释放cpu的时间片,这几天写了个基于Event通知的线程,应用起来还不错,就帖上来,希望大家多多指教.
//------------------------------------------
// 处理数据线程父类,实现方法由子类完成
//------------------------------------------
TPlayThread =class(TThread)
private
HEvent: THandle.
CS_Memory:TRTLCriticalSection .
FData: array of byte.
FManager:TManager.
FMemory: TMemoryStream .
FReadPos: integer.
procedure play. virtual.abstract.
function ReadData: Boolean. virtual.
procedure ClearData.
public
constructorcreate(AManager: TManager).
destructor destroy. override.
procedure execute. override.
procedure WriteData(var ABuffer. ASize:DWORD). virtual .
end.
//视频数据处理线程
TVideoPlayThread =class(TPlayThread)
private
procedure play. override.
end.
//音频数据处理线程
TAudioPlayThread = class(TPlayThread)
private
procedure play. override.
end.
//礼物数据处理线程
TPresentPlayThread = class(TPlayThread)
private
CHAT_Present: TCHAT_Present.
function ReadData: Boolean. override.
procedure play. override.
public
procedure WriteData(varABuffer. Asize: DWORD). override.
end.
implementation
//以下为功能实现部分
{ TPlayThread }
procedureTPlayThread.ClearData.
begin
FReadPos := 0.
FMemory.Clear.
end.
constructor TPlayThread.create(AManager: TManager).
begin
InitializeCriticalSection(CS_Memory).
HEvent :=CreateEvent(nil, False, False, nil).
FManager := AManager.
FMemory:= TMemoryStream.Create.
FreeOnTerminate := true.
inheritedCreate(false).
特别推荐:
2009年9月全国计算机等级考试时间及科目预告