Recent

Author Topic: Circular unit reference with Unit and Thread  (Read 821 times)

wytwyt02

  • Jr. Member
  • **
  • Posts: 74
Circular unit reference with Unit and Thread
« on: January 16, 2020, 03:47:36 am »
I have a mainForm.pas and a thread.pas.

 mainForm.pas call thread.pas to create thread, and thread.pas will update ui in mainForm. so the two files are use each other. The prolem of Circular unit reference will occurs.

I do not wanna to combine two file to one, because of make my project clear.

zamronypj

  • Jr. Member
  • **
  • Posts: 58
    • Fano Framework, Free Pascal web application framework
Re: Circular unit reference with Unit and Thread
« Reply #1 on: January 16, 2020, 04:11:43 am »
You can break circular unit reference, by moving unit uses to implementation section if it possible. However, without example code, I cannot comment much.
Fano Framework, Free Pascal web application framework https://fanoframework.github.io
Personal Projects https://v3.juhara.com
Github https://github.com/zamronypj

del

  • Full Member
  • ***
  • Posts: 162
Re: Circular unit reference with Unit and Thread
« Reply #2 on: January 16, 2020, 04:43:11 am »
Is there a reason or reasons why unit references aren't put in the implementation section as a matter of standard practice - and only put into the interface section if necessary? That's the way I do it.

wytwyt02

  • Jr. Member
  • **
  • Posts: 74
Re: Circular unit reference with Unit and Thread
« Reply #3 on: January 16, 2020, 08:33:46 am »
For example:

mainForm.pas:

 
Code: Pascal  [Select]
  1.  
  2. type
  3.  
  4. TForm1 = class(TForm)
  5.     Label1: TLabel;
  6.   private
  7.  
  8.   public
  9.  
  10.   end;

thread.pas:


 
Code: Pascal  [Select]
  1. type
  2.  EncThread = class(TThread)
  3.  
  4.   private
  5.     aFileNamePath: string;
  6.     aForm: TForm1;
  7.   public
  8.     procedure Execute; override;
  9.     constructor Create(aAForm: TForm1; aFileName: string);
  10.     procedure ChangeLableStatus;
  11.   end;
  12.  

If i put uses in implementation section, the above type section will throw error, because `EncThread` contains `TForm1` type from mainForm.pas

wytwyt02

  • Jr. Member
  • **
  • Posts: 74
Re: Circular unit reference with Unit and Thread
« Reply #4 on: January 16, 2020, 09:09:24 am »
Is there a reason or reasons why unit references aren't put in the implementation section as a matter of standard practice - and only put into the interface section if necessary? That's the way I do it.

Yes, it is confused

wp

  • Hero Member
  • *****
  • Posts: 6726
Re: Circular unit reference with Unit and Thread
« Reply #5 on: January 16, 2020, 10:35:37 am »
Do you really need to specify AForm as TForm1 in the constructor of EncThread? For example, when you only want to redraw the form it is enough to have its ancestor TForm because TForm already has the Invalidate method, and the circular reference is resolved.

When you want to do something specific with a single control on TForm1, e.g. update the Text of a TEdit, you should only pass the TEdit to EncThread.Create.

If nothing like this works out (but I doubt this) you must add the thread class to the TForm1 unit.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

wytwyt02

  • Jr. Member
  • **
  • Posts: 74
Re: Circular unit reference with Unit and Thread
« Reply #6 on: January 23, 2020, 01:55:03 am »
Do you really need to specify AForm as TForm1 in the constructor of EncThread? For example, when you only want to redraw the form it is enough to have its ancestor TForm because TForm already has the Invalidate method, and the circular reference is resolved.

When you want to do something specific with a single control on TForm1, e.g. update the Text of a TEdit, you should only pass the TEdit to EncThread.Create.

If nothing like this works out (but I doubt this) you must add the thread class to the TForm1 unit.

That's not convenient for organization code, I wish Free Pascal should support Circular unit reference

VTwin

  • Hero Member
  • *****
  • Posts: 855
  • Former Turbo Pascal 3 user
Re: Circular unit reference with Unit and Thread
« Reply #7 on: January 23, 2020, 03:06:11 am »
You could try having the thread send a message that the mainform would act on.
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn r62669 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

vangli

  • New Member
  • *
  • Posts: 21
Re: Circular unit reference with Unit and Thread
« Reply #8 on: January 23, 2020, 08:32:24 am »
Could it be better to putt your TForm1 type in a separate unit and let Mainpas.pas and thread.pas reference that unit. Remember also that the Form object you may have created in Mainpas.pas is not the same object as the one you creates in thread.pas.

If you really want to pass information back to your mainpas.pas form, please read https://wiki.lazarus.freepascal.org/Multithreaded_Application_Tutorial . It explains lot of things to be beware of using threads.

Good luck  :)
Regards Bent