代码如下: MessageDlg('打印前将自动存为草稿,是否继续?', System.UITypes.TMsgDlgType.mtInformation,[TMsgDlgBtn.mbYes,TMsgDlgBtn.mbCancel], 0, procedure(const AResult: TModalResult) begin if AResult = mrYES then begin if Assigned(Mythreadtask) then begin if Mythreadtask.Status = TTaskStatus.Running then begin //If it is already running don't start it again Exit; end; end; Mythreadtask := TTask.Create(procedure() begin try ClientModule1.Fc_Server_SM_FmClient.RemoPrint(billvchcode); except On E:Exception do showmessage(e.Message); end; end);
exists any response from server when the print is OK? or not?
for example: if "RemoPrint(xxxxx)" = false/400 then ... "error"... wait a time and send again new try...
thread EXECUTE: ... while Not Terminated do begin try LResult:= RemoPrint(xxxxxxx); // if LResult then exit; // OK it was send to print... except // what to do if any exception? continue or exit? // reset printer....? send again? exit? wait a time? end; // sleep( 5000 ); // wait a time for example to new try? end;
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
Mythreadtask := TTask.Create(procedure() begin try ClientModule1.Fc_Server_SM_FmClient.RemoPrint(billvchcode); except On E:Exception do showmessage(e.Message); end; end);
if ClientModule1.Fc_Server_SM_FmClient.RemoPrint('13') =1 then begin SHOWMESSAGE('单据打印完成!'+billvchcode); exit; end; except On E:Exception do showmessage(e.Message); end;
不放到线程里执行,打印任务可以完成,但是会提示:Read timed out 然后App就闪退了。 这里的Read Time 是指的哪里的Time out 呢?
----------------------------------------------
偶尔做做代码应付一下工作,却发现Delphi已成必配
1) im using a PDF Printer for tests ( i dont have any printer in my MSWin)
2) im using "Printer" object (TPrinter class) to send a text to printer
3) im using "StrToInt("hello") to cause an ERROR = Exception for tests
4) im using "InputQuery" to ask to user "yes" or "no" for re-try printing
5) im using "Execute" procedure in my MyThread to controls the print task or inputquery ask to user an answer
6) im using "Printer.Abort" to cancel the printing if user desire...
7) all exception is controlled in my "Try...Except...End" block, then the "MyThread" not end until the user say: NO or press "ESC" or press "CANCEL" button on "InputQuery" or press "Abort" button on the form!!!!
in my video you can that "Printer Queue" receive the files to printing ....
IMPORTANT NOTE: While the "InputQuery is on the screen" YOU CANNOT KILL/RELEASE THE THREAD!
Then try use a "MODAL" dialog to avoid access the button "Abort or Free" the thread!!!!
see
此帖子包含附件: 大小:531.7K
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3
try //此处需要注释掉,每次调用远程打印没有问题 sql:=' select BaseNo from B_Person where BaseNo=1 '; ClientModule1.ClientDataSet1.Close; ClientModule1.ClientDataSet1.CommandText:=sql; ClientModule1.ClientDataSet1.Open; //注释到这里,中间这段 TThread.CreateAnonymousThread(procedure() begin try if ClientModule1.Fc_Server_SM_FmClient.RemoPrint('13')=1 then begin TThread.Synchronize(TThread.CurrentThread,procedure() begin SHOWMESSAGE('单据打印完成! '+billvchcode); end); end;
except On E:Exception do TThread.Synchronize(TThread.CurrentThread,procedure() begin showmessage('1:'+e.Message);// SHOWMESSAGE('单据打印任务失败 ! '+billvchcode); end);
end; end).start;
except On E:Exception do showmessage('2:'+e.Message); end;
2. ClientModule1.Fc_Server_SM_FmClient.RemoPrint 这句是由线程执行的。这行代码内部究竟是如何工作的,我不知道。但它是否线程安全?比如,如果是在 Windows 底下,你调用的这个方法,它可能用到 WINDOWS 的 COM 控件(比如 ADO 数据库访问),那么,线程调用这样的代码,就需要先执行一次 COM 同步操作的语句。你这里是安卓,有没有其它需要注意的,你要自己查。
----------------------------------------------
-
1) if the amount of data (records) read from the table is little or moderate, then you could do the "looping" in the table and store these values in an "array"/"list", etc... or even in an ordinary string! This way, you avoid having to access the database during the execution of the thread!
for example:
1) reading table records; var // or any other way if there are many fields.... LMyData:TArray<string>; // TDictionary / TList / TStringList / etc... begin tablex.Open( 'select xxxx from tbHello where xField = yyyyy') ; // etc... while NOT tablex.EOF do begin // storing data for each record... LMyData := LMyData + [ MyFieldValue.AsString ]; tablex.NEXT; end; // tablex.CLOSE;
2) creating the thread and sending the previously read data
MyThread.Create( LMyData .... ); .... process thread with "LMyData" ..... printing data from "LMyData".......
MyThread BYE BYE
----------------------------------------------
The higher the degree, the greater the respect given to the humblest!RAD 11.3