Форум: "Начинающим";
Текущий архив: 2019.02.03;
Скачать: [xml.tar.bz2];
ВнизPostThreadMessage и GetMessage Найти похожие ветки
← →
eh (2016-10-25 17:32) [0]Здравствуйте, уважаемые мастера!
Из одного потока отсылаю сообщения в другой с помощью 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
← →
Юрий Зотов © (2016-10-25 17:55) [1]TranslateMesssage ?
← →
eh (2016-10-25 19:39) [2]Добавил 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;
← →
eh (2016-10-25 19:50) [3]Торможу :)
Сообщение должно быть WM_USER + I. Тогда всё работает.PostThreadMessage(T.ThreadID, WM_USER + I, 0, 0);
← →
Игорь Шевченко © (2016-10-25 23:34) [4]Справка настоятельно рекомендуется к чтению
" 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-26 08:49) [5]Спасибо
Страницы: 1 вся ветка
Форум: "Начинающим";
Текущий архив: 2019.02.03;
Скачать: [xml.tar.bz2];
Память: 0.45 MB
Время: 0.001 c