Threading Issues

1) This was probably asked a lot earlier, and I read a lot about Sleepvs WaitForSingleObject, but still got a little confused. As an example, I have a simple background thread that is called from a data stream to show a message to the user without blocking the data stream. What in terms of performance (CPU load / CPU time) in this case is better: http://pastebin.com/VuhfZUEg or http://pastebin.com/eciK92ze I suspect that the time is shorter Sleepand the more flags I have, the worse the performance. On the other hand, with a longer time.Sleepthe performance will be better, but the reaction delay will increase, and with a large number of reaction delay the reaction will eventually become noticeable to the user on the younger machine. It's right? So, what is the best way to keep inactive, inactive threads?

2) Lock SetEvent( SendMessage) or non- block ( PostMessage)?

3) In the event TForm.OnCreate, I have the following code:

procedure TFormSubsystem.FormCreate(Sender: TObject);
begin
  Application.OnException:=LogApplicationException;
  Application.OnActivate:=InitiateApplication;
end;

procedure TFormSubsystem.LogApplicationException(Sender: TObject; E: Exception);
var
  ErrorFile: TextFile;
  ErrorInfo: String;
begin
  AssignFile(ErrorFile, AppPath+'Error.log');
  if FileExists(AppPath+'Error.log') then
    Append(ErrorFile)
  else
    Rewrite(ErrorFile);

    ErrorInfo:=DateTimeToStr(Now)+' Unhandled exception';
    if Assigned(Sender) then
      ErrorInfo:=ErrorInfo+' in '+Sender.UnitName+'->'+Sender.ClassName;
    ErrorInfo:=ErrorInfo+': '+E.Message;

  try
    WriteLn(ErrorFile, ErrorInfo);
  finally
    CloseFile(ErrorFile)
  end;
end;

This is not the best way to report errors, but it is simple. The question arises: what happens if TSomeThreadAncestor.Executethere is an exception or inside the method called through Synchronize?

4) What is the difference between Synchronizeand Queue? Which should I use when my background threads interact with the GUI? I don’t have race conditions and already use something like semaphores.

5) ?

procedure TShowBigDialoxBoxThread.Execute;
begin
  while ThreadNotTerminated do begin
    EventHandler.WaitFor(INFINITE);
    if not(ThreadNotTerminated) then
      Continue;
    EventHandler.ResetEvent;
    Synchronize(procedure begin
      MessageDlgBig(FMsg, FDlgType, FButtons, FHelpContext, FDefaultButton, FDlgMinWidth);
    end); // this kind of Synchronize call looks fishy
  end;
end;

, ?

: Delphi XE5.

+4
1

, . , , . , , ( , ).


:

:

, "()" , . ( ) , .. , - , , "" (), . , , , , "()" 2 ().

Blocking:

, "()" , , , . .


: 'SetEvent()' , .

+2

Source: https://habr.com/ru/post/1589210/


All Articles