我的程序,有一个用计时器做的捕捉图片,生成文件
另外开了一个线程,上传图片到服务器上.现在出现问题,当上传图片的线程还没有停止操作图片时,
捕捉图片已经开始再次刷新文件了,这个时候会出现错误.
要怎么处理,才能使捕捉图片和上传图片不会有冲突,但是放到同一个线程
会使画面有停顿的感觉,怎么样处理比较合适,期待探讨
另外开了一个线程,上传图片到服务器上.现在出现问题,当上传图片的线程还没有停止操作图片时,
捕捉图片已经开始再次刷新文件了,这个时候会出现错误.
要怎么处理,才能使捕捉图片和上传图片不会有冲突,但是放到同一个线程
会使画面有停顿的感觉,怎么样处理比较合适,期待探讨
mutexhandle:thandle;
begin
mutexhandle:=CreateMutex(nil,false,nil);
WaitForSingleObject(mutexhandle,INFINITE);
//在这写代码;
ReleaseMutex(mutexhandle);
end;
do
caputre;
SetSendThreadEvent; // 令发送线程开始工作
WaitforSingleObject(CaptureEvent, INHRITED); // 等待发送完成
Sleep(2000); // 你 定时器的时间
until not Terminated;发送线程do
WaitforSingleObject(SendEvent); // 等待采集线程通知自己可以发送
SendTo; //发送
SetCaptureThreadEvent; //通知采集线程已发送完毕,要求继续采集
until not Terminated;
上面是大致的流程,当然还涉及一些具体的条件判断、异常处理,比如你手动终止采集、发送失败之类的
hthread := CreateThread(nil, //Security attribute
0, //Initial Stack
@ThreadFunc, //上传函数
nil, //argument of thread
0, // Create flags
ThreadID); // thread ID
if hthread = 0 then
begin
showMessage('无线程! ');
end;上传函数:
procedure ThreadFunc(Flag: boolean) stdcall;
var
setUpFile : Tinifile;
ftpAddress : string;
ftpUname : string;
ftpPwd : string;
ftpPicPath : string;
led_Id : string;begin
setUpFile := Tinifile.create(extractfiledir(application.ExeName)+'\setup.ini');
ftpAddress := setUpFile.readstring('FTP地址','ftp_address','');
ftpUname := setUpFile.readstring('FTP用户名','ftp_uname','');
ftpPwd := setUpFile.readstring('FTP登陆密码','ftp_pwd','');
ftpPicPath := setUpFile.readstring('FTP图片地址','ftp_pic_path','');
led_Id := setUpFile.readstring('屏号','screenid','');
setUpFile.free;
with DLink_Form.idftp1 do
try
Username:= ftpUname;
Password:= ftpPwd;
Host:= ftpAddress;
//passive:=true;
if not Connected then
begin
connect;
end;
except
showmessage('程序异常退出');
end; if DLink_Form.IdFtp1.Connected then
begin
//DLink_Form.idftp1.Delete(DLink_Form.idftp1.RetrieveCurrentDir+ftpPicPath+led_Id+'.jpg');
DLink_Form.idftp1.Put(extractfiledir(application.ExeName)+'\test.jpg',DLink_Form.idftp1.RetrieveCurrentDir+ftpPicPath+led_Id+'.jpg',false);
DLink_Form.idftp1.disconnect;
end
else
begin
application.MessageBox('FTP没有连上!','提示:');
end;
Play_Form.mainTemp := 0; //全局变量,在TIMER里面判断它,然后做捕捉图片的程序。
end;
执行几次,有时是几分钟,有时长点时间,到idftp1.Put的时候就出现错误了
EIdprotocolRelyError with message Port Command的提示网上搜过几个答案,我把idftp1.passive := true;会出现连接错误现在不知道怎么解决这个问题了,前面几位大虾提出的建议不好用,能帮忙看看代码然后指点一下吗