2017-10-12 47 views
-2

我開發的應用程序,使用Delphi 6和異步免費德爾福的傳輸延遲

通過虛擬串行端口傳輸字節我需要延遲傳輸10個字節後同步傳輸。我使用windows.sleep(1)

當我從Delphi IDE運行它時,該應用程序運行良好。當我關閉Delphi並從exe運行應用程序時...應用程序變得很慢。

是什麼原因?

+0

目標設備不理解你,也許? – Victoria

+0

我成功實現了很多次......並且我真的無法理解這個問題......當打開Delphi時,傳輸速度非常快並且工作得很好......當關閉它並運行exe時......傳輸速度很慢 –

+1

我不知道爲什麼downvotes? –

回答

2

Delphi IDE顯然會將系統計時器記號設置爲更高的分辨率。您可以使用timeBeginPeriod/timeEndPeriod函數在應用程序中執行相同的操作。見msdn documentthis 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; 

只是爲了演示下面這個簡單的應用程序,我取得了效果,所以任何人都感興趣的可以檢查自己:

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; 

輸出:

with 1 
without 15 

注意因爲th e TimeBeginPeriod會影響系統時鐘,請確保關閉任何程序,可以使用同樣的方法修改計時器記號,例如多媒體和類似程序(以及Delphi IDE)。

+0

更好的文檔可能是[Sleep](https://msdn.microsoft.com/en-us/library/ms686298(v = vs.85).aspx),它描述了這兩個函數之間的關係。 –

+0

我並沒有質疑你答案的準確性,我知道你說的是一個可能的原因。 –

+0

@Sertac感謝您的意見。我的第一個測試是15000次迭代(ms)和'timeBeginPeriod'循環,耗時15秒,沒有231秒!這是永遠的:) –