-2

I developed application that transfer bytes via virtual serial port using Delphi 6 and asyncfree

I need to delay transfer after 10 bytes to synchronize transmission. I use windows.sleep(1).

The application works very well when I run it from Delphi IDE. When I close Delphi and run application from exe ... the application becomes slow.

What is the reason?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • The target device doesn't understand you, maybe? – Victoria Oct 12 '17 at 19:05
  • I did successed transmition many times... and really I couldn't understand the problem... When open Delphi the transmission is fast and works very well... When just close it and run exe... The transmition is slow – Koky Crochet Oct 12 '17 at 19:13
  • 1
    I wonder why the downvotes? – Tom Brunberg Oct 12 '17 at 22:52
  • 2
    @Tom - I can only speak for mine of course; but AAMOF your answer is a shot in the dark, nothing is given in the question. For experienced programmers that's a shot with a decent probability of course, I had the exact same one [here](https://stackoverflow.com/questions/3435083/delphi-multithreaded-application-built-in-vista-and-wont-run-in-xp), Nat had the exact same one [here](https://stackoverflow.com/questions/6139026/serial-communication-rts-and-windows-7). But intuition of the answerer does not substitute for the quality of the question. – Sertac Akyuz Oct 12 '17 at 23:29
  • @SertacAkyuz: Well said. I may ask for your permission to use that last sentence (*But intuition*) in the future. – Ken White Oct 13 '17 at 02:48
  • 1
    @Sertac, *AAMOF ... a shot in the dark, nothing ... in question*. Please let me object. Specifically two details in q gives AAMOF the required clues, usage of `sleep()` and difference when run in IDE and when Delphi is closed. Anybody that has been dealing with this before, sees the connection. Unfortunately the link to the Emba forum discussion in your reference doesn't work anymore but looking in my archive i guess it is your post Sept. 10, 2009 *Duration of Sleep(1) call whether Delphi IDE is open or not* you linked to. I'm sure you knew immediately when you read this q what the reason is. – Tom Brunberg Oct 13 '17 at 06:04

1 Answers1

2

The Delphi IDE obviously sets the system timer tick to a higher resolution. You can do the same in your application by using timeBeginPeriod/timeEndPeriod functions. See msdn document and also this one regarding sleep function

uses MMSystem;


  if TimeBeginPeriod(1) = TIMERR_NOERROR then // 1 ms resolution
  try
    // The action or process needing higher resolution
  finally
    TimeEndPeriod(1);
  end;

Just to demonstrate the effect I made following simple application, so anybody interested can check for themselves:

uses System.DateUtils, MMSystem;

var
  s, e: TTime;

procedure SomeDelay;
var
  i: integer;
begin
  s := Now;
  for i := 1 to 1000 do
    Sleep(1);
  e := Now;
end;

procedure TForm19.btnWithClick(Sender: TObject);
begin
  if TimeBeginPeriod(1) = TIMERR_NOERROR then // 1 ms resolution
  try
    SomeDelay; // The action or process needing higher resolution
  finally
    TimeEndPeriod(1);
  end;

  Memo1.Lines.Add('with '    + IntToStr(SecondsBetween(s, e)));
end;

procedure TForm19.btnWithoutClick(Sender: TObject);
begin
  SomeDelay; // The action or process needing higher resolution
  Memo1.Lines.Add('without ' + IntToStr(SecondsBetween(s, e)));
end;

Output:

with 1
without 15

NOTE Because the TimeBeginPeriod affects the system tick, be sure to shut down any program that may us the same method to modify the timer tick, like multimedia and similar programs (and also the Delphi IDE).

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54