![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
PostThreadMessage и GetMessage
eh © (2016-10-25 17:32:00)Здравствуйте, уважаемые мастера!
Из одного потока отсылаю сообщения в другой с помощью PostThreadMessage. Но во втором потоке часть сообщений теряется.
Подскажите, пожалуйста, в чём дело.
unit Unit1;
interface
uses
uEventLog,
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TT = class(TThread)
protected
procedure Execute; override;
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
T: TT;
public
//
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TT.Execute;
var
MSG: TMSG;
begin
while GetMessage(MSG, 0, 0, 0) do
begin
EventLog.WriteString(IntToStr(MSG.Message));
DispatchMessage(MSG);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
T := TT.Create(False);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
for I := 1 to 50 do
PostThreadMessage(T.ThreadID, I, 0, 0);
end;
end.
В лог пишется следующее:
2
3
4
5
6
7
8
9
10
11
15
16
17
eh (2016-10-26 08:49:00) [1]Спасибо
Игорь Шевченко (2016-10-25 23:34:00) [2]Справка настоятельно рекомендуется к чтению
" As a general rule, messages that are not associated with a window cannot be dispatched by the DispatchMessage function."
https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms644946(v=vs.85).aspx
> В лог пишется следующее:
В общем-то верхний предел вполне разумен - WM_QUIT.
"If hWnd is -1, GetMessage retrieves only messages on the current thread's message queue whose hwnd value is NULL, that is, thread messages as posted by PostMessage (when the hWnd parameter is NULL) or PostThreadMessage."
https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms644936(v=vs.85).aspx
eh (2016-10-25 19:50:00) [3]Торможу :)
Сообщение должно быть WM_USER + I. Тогда всё работает.
PostThreadMessage(T.ThreadID, WM_USER + I, 0, 0);
eh (2016-10-25 19:39:00) [4]Добавил TranslateMessage. Результат тот-же :(
procedure TT.Execute;
var
MSG: TMSG;
begin
while GetMessage(MSG, 0, 0, 0) do
begin
EventLog.WriteString(IntToStr(MSG.Message));
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end;
Юрий Зотов (2016-10-25 17:55:00) [5]TranslateMesssage ?