Strange UI Text behaviour

Just got this strange error (Strange UI Text behaviour - Questions & Answers - Unity Discussions)

(X) Error occured: get_isActiveAndEnabled can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function./ at (wrapper managed-to-native) UnityEngine.Behaviour.get_isActiveAndEnabled(UnityEngine.Behaviour)
at UnityEngine.EventSystems.UIBehaviour.IsActive () [0x00000] in D:\Unity Game Engine\2020.1.6f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\EventSystem\UIBehaviour.cs:28
at UnityEngine.UI.Graphic.SetVerticesDirty () [0x00000] in D:\Unity Game Engine\2020.1.6f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\UI\Core\Graphic.cs:267
at UnityEngine.UI.Text.set_text (System.String value) [0x0003d] in D:\Unity Game Engine\2020.1.6f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\UI\Core\Text.cs:212
at ChatController.Print (System.String msg) [0x0007e] in D:\Untitled Sandbox\Assets\Resources\Multiplayer\ChatController.cs:60
at ClientController.ProcessMessage (System.Net.Sockets.NetworkStream stream) [0x00184] in D:\Untitled Sandbox\Assets\Resources\Multiplayer\ClientController.cs:103
at ClientController.MessageProcessLoop () [0x00019] in D:\Untitled Sandbox\Assets\Resources\Multiplayer\ClientController.cs:45

Okay, MessageProcessLoop() is self-explanatory.
ProcessMessage() is also self-explanatory. It calls Print() funciton.
Print() just adds a text into list, and then applies it to text UI object.
And then this error comes out.

Any ideas?

It looks like you have some kind of asynchronous network listener happening in MessageProcessLoop running in a different thread. You can’t access most Unity objects from any thread except the main thread. You need to pass your data into the main thread somehow and then interact with Unity objects such as UI text.

Gimme a check

Ohh, Task.Delay() Method is asynchronous one.

Just when I try to loop every frame, it crashes.
When I try to loop on fixed amount of frames, it crashes. And i decided just use Task.Delay().ContinueWith( () => /* Some things here */ ).

Now I’ll use IEnumerator instead.

Thanks!