0

I have one messagehandler in the form like this:

procedure TMain_Form.form_message_handler(var MSG: TMessage);
begin
  case MSG.WParam of
   0: global_variable:=10;
   1: global_variable:=global_variable+100;
   end;
end;

Several threads will send to it asynchronous messages - PostMessage. Is manipulation of global variables (within such a handler) safe - I mean that access to these variables is safe? I will plan to manipulate this global variables only inside this handler. I assume it is safe because the messages handled by the handler waiting for execution in the queue. Is my assumption is correct?

Artik
  • 803
  • 14
  • 36

1 Answers1

0

Not really, no. If you are going to send asynchronous messages via. PostMessage, (not that it's a bad idea - hugely better than the apalling TThread.Synchronize), try very hard to post ALL of the data required by the message-handler, ie. do not use globals. If you have to communicate a lot of stuff, post a struct or object pointer in wParam/lParam.

Do not use globals unless... nothing really.

Oh, and another thing - do not use globals.

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • @Maritn James:Yes, I do not use global variables for sending information to handler. Instead of it, I use pointer to struct of data inside wParam/lParam like you say (maybe my question is not enough clear -I'm sorry). After sending information to hadler via i.e. l/Param I want to mainpulate global data only inside this handler. I calculate and write all results in one global variable using critical section (it guarantees enough short time - only one variable). This variable can be use (using the same critical section) by another one thread using in tcp communication. Is it in general correct? – Artik May 14 '13 at 04:12