Recent

Author Topic: I don´t understands Sincronize  (Read 3248 times)

CarlLanco

  • Newbie
  • Posts: 6
I don´t understands Sincronize
« on: August 22, 2017, 09:11:37 pm »
I am trying to make a multithreaded application in which a background thread performs some calculations while the main window shows a graph of the data. Each time the background thread finishes an iteration, it must return the results to the main window.
I understand that for this I should use Synchronize in the thread in the background, but where in the main thread should I put something so that this one finds out that the data arrives?


taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: I don´t understands Sincronize
« Reply #1 on: August 22, 2017, 09:26:51 pm »
Synchronize is a mechanism that forces the method you pass to it to run inside the main thread context. It does not pass any kind of data. For simplicity you can think of it as a flag that informs the main thread that it needs to run the method you specified and the thread that raised the flag waits until the main thread resets the flag to 0.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Lulu

  • Full Member
  • ***
  • Posts: 226
Re: I don´t understands Sincronize
« Reply #2 on: August 22, 2017, 09:36:33 pm »
Hello,
I also use Synchronize in a thread to update some visual component in main form.
I use this kind of structure:
Code: Pascal  [Select][+][-]
  1. // THREAD LOOP
  2. while not Terminated do
  3.  begin
  4.  CalcResult := some calculations...;
  5.  Synchronize( @UpdateProc );
  6.  end;
  7.  
  8. // and in your UpdateProc, simply update visual component in main form like
  9. procedure MyThread.UpdateProc;
  10. begin
  11.  MainForm.Label1.Caption := inttostr( CalcResult );
  12. end;
  13.  
wishing you a nice life

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: I don´t understands Sincronize
« Reply #3 on: August 22, 2017, 10:52:17 pm »
I am trying to make a multithreaded application in which a background thread performs some calculations while the main window shows a graph of the data. Each time the background thread finishes an iteration, it must return the results to the main window.
I understand that for this I should use Synchronize in the thread in the background, but where in the main thread should I put something so that this one finds out that the data arrives?

Yes, you should keep the mainthread as light as possible, and quickly return from any event handler.

CarlLanco

  • Newbie
  • Posts: 6
Re: I don´t understands Sincronize
« Reply #4 on: August 23, 2017, 05:35:49 pm »
OK, understand, Lot of thanks, guys.. I will try and  expands news...

CarlLanco

  • Newbie
  • Posts: 6
Re: I don´t understands Sincronize
« Reply #5 on: August 25, 2017, 10:14:20 pm »
New problem...
I understand that when I use Synchronize (@MyMethod), MyMethod is a TMyThread procedure, but when I use MainForm variables in MyMethod , the compiler returns an error: Identifier not found "MainForm" ...

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: I don´t understands Sincronize
« Reply #6 on: August 25, 2017, 10:38:03 pm »
Have you added the mainform unit to the uses clause of your thread unit? Preferably in the implementations uses clause.
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

CarlLanco

  • Newbie
  • Posts: 6
Re: I don´t understands Sincronize
« Reply #7 on: August 25, 2017, 11:07:36 pm »
Thanks, that works.. but now MyForm.pas(130,28) return : "Error: Variable identifier expected " in Synchronize(@MyMethod)

CarlLanco

  • Newbie
  • Posts: 6
Re: I don´t understands Sincronize
« Reply #8 on: August 26, 2017, 12:18:42 am »
Thanks, that works.. but now MyForm.pas(130,28) return : "Error: Variable identifier expected " in Synchronize(@MyMethod)

 It's fine, i use Synchronize(MyMethod) without "@" and work very well

Thanks al lot, guys..

 

TinyPortal © 2005-2018