title: Is PeekMessage sufficient to query the message queue for mouse button input?
body: <p>Our application deals with measurement data which has to be read in from a measurement device and is stored in a database.</p>
  
  <p>We provide the option to bulk read &amp; store many sets of measurement data at a time. Because this is a time consuming process we display a modal dialog box with a progress bar and a button to cancel the operation.</p>
  
  <p>It should only be possible to cancel the operation after a complete set of measurement data has been read and stored.</p>
  
  <p>The read &amp; store loop is as follows:</p>
  
  <pre><code>ItemsToStore := GetSelectedTreeItems();
  DlgProgress  := TProgressWithAbort.Create(Screen.ActiveForm);
  
  try
    for i := 0 to Pred(ItemsToStore.Count) do
    begin
      if DlgProgress.Cancel then exit;
  
      DlgProgress.Description := ItemsToStore[i].Name;
      ReadAndStoreItem(ItemsToStore[i].Id);
  
      DlgProgress.Position := Succ(i) * 100 div ItemsToStore.Count;
    end;
  
  finally
    DlgProgress.Free;
  end;
  </code></pre>
  
  <p>The setter of the progress dialog's Position property calls a procedure named CheckMouseButtonInput which is currently coded as follows:</p>
  
  <pre><code>procedure TProgressWithAbort.CheckMouseButtonInput;
  var
    Msg: TMsg;
  
  begin
    // if the left mouse button was pressed while the mouse was at the
    // Cancel button call the application's message loop to process the event
    if PeekMessage(Msg, btnCancel.Handle, WM_LBUTTONUP, WM_LBUTTONUP, PM_NOREMOVE) then
      Application.ProcessMessages;
  end;
  </code></pre>
  
  <p>From the application's message loop the following button click handler is called which sets the variable that is accessible via the property Cancel:</p>
  
  <pre><code>procedure TProgressWithAbort.btnCancelClick(Sender: TObject);
  begin
    FCancel := true;
  end;
  </code></pre>
  
  <p>Everything works fine. But I wonder if the above implementation of CheckMouseButtonInput maybe consumes too much CPU time. Would it be better to call <code>GetQueueStatus</code> or <code>MsgWaitForMultipleObjects</code> (with no handles and a timeout of 0) before <code>PeekMessage</code>?</p>