Real-Serious-Games / C-Sharp-Promise

Promises library for C# for management of asynchronous operations.
MIT License
1.19k stars 149 forks source link

get_name can only be called from the main thread. #89

Closed FernandoMonroy closed 5 years ago

FernandoMonroy commented 5 years ago

I got the following issue. It's not presented always, but y appears randomly and stops the execution of the program.

For now it has been show on Editor, multiple times.

get_name 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. UnityEngine.Object:get_name() BootstrapStageLogger:Logger(String, AnalyticsDataStage) (at Assets/_CommonFMM/Scripts/Bootstrap/BootstrapStage.cs:17)

c__AnonStorey1:<>m__0(Task`1) (at Assets/_CommonFMM/Scripts/Pipeline/BootstrapServices/AnalyticsDataStage.cs:49) System.Threading.Tasks.c__AnonStorey0:<>m__0(Object)
RoryDungan commented 5 years ago

From the look of your stack trace it appears that your problem is actually inside Unity, not in the promise library. Since promises have no concept of threading inherintly but are just a useful tool that can be used for managing tasks across threads, you still need to keep track of what threads you're using promises from yourself.

If you aren't already using this, the dispatcher pattern can be quite useful when managing tasks that run on different threads, and it fits nicely with promises. Unity also has limitations on which threads can access certain data so make sure that you aren't calling any Unity functions off the main thread that it doesn't support. One solution to this is to cache the data you need on the main thread and then pass that to the dispatch thread that will do the actual processing on it.