关于控件库中的Comm32?(100分)

X

xjf

Unregistered / Unconfirmed
GUEST, unregistred user!
Comm32控件采用了多线程技术,定义了ReadThread 和 WriteTHread
本人在调试程序时发现线程冲突,并造成Access violation错误.
具体如下:
在Startcom函数中,
// Create the Read thread.
try
ReadThread := TReadThread.Create( True {suspended} );
except
LogDebugLastError( GetLastError, 'Unable to create Read thread' );
raise ECommsError.Create( 'Unable to create Read thread' );
end;
ReadThread.hCommFile := hCommFile;
ReadThread.hCloseEvent := hCloseEvent;
ReadThread.hComm32Window := FHWnd;
ReadThread.Resume;

ReadThread.Priority := tpHighest;

// Create the Write thread.
try
WriteThread := TWriteThread.Create( True {suspended} );
except
LogDebugLastError( GetLastError, 'Unable to create Write thread' );
raise ECommsError.Create( 'Unable to create Write thread' );
end;
WriteThread.hCommFile := hCommFile;
WriteThread.hCloseEvent := hCloseEvent;
WriteThread.hComm32Window := FHWnd;
WriteThread.Resume;

当写线程Resume,就发生错误.此时读线程正在进行读操作.
请问如何修正?
 
贴上一段代码,希望有帮助,省略号的部分与你的代码对应
(请注意priority的设置与resume的位置及顺序)


try
ReadThread := TReadThread.Create( True {suspended} );
except
.
.
.
raise ECommsError.Create( 'Unable to create read thread' )
end;
.
.
.

ReadThread.Priority := tpHighest;

// Create the Write thread.
try
WriteThread := TWriteThread.Create( True {suspended} );
except
.
.
.
raise ECommsError.Create( 'Unable to create write thread' )
end;
.
.
.

WriteThread.Priority := tpHigher;

ReadThread.Resume;
WriteThread.Resume

如果成功,请说一声。
 
现在发现我用的版本是1.0的,有很多Bug,换用Spcomm控件,一切正常.
谢谢dwwang的回答.大家真是非常热心.
 
老兄果然冰雪聪明,我那段代码就是从Sp_Comm趸来的:)
哈哈!
 
顶部