A
A
aerdev2019-01-20 05:00:05
C++ / C#
aerdev, 2019-01-20 05:00:05

How to bypass the Task limit in C#?

Hello. I'm trying to write a game using Google's Firebase. Please tell me how can I solve this problem.
There is code:
FirebaseDatabase.DefaultInstance.GetReference("Leaders").GetValueAsync().ContinueWith(task => {
if (task.IsFaulted) {
// Don't look here...
}
else if (task.IsCompleted) {
DataSnapshot snapshot = task.Result;
// Look here and assume that I pulled some STRING from the snapshot...
Debug.Log(STRING); // works
Text.text = STRING; // doesn't work
}
});
In short, this asynchronous task prevents me from changing the text in the Unity Text UI element. And, everything is written to the console normally. The exception handler says that changing this very text can only occur in the main thread. But I'm not that simple. I have crutches. I assign this STRING to the declared variable string and on the main thread I write method1 that transfers it to Text.text. Only because of the asynchrony of this ContinueWith, the data does not have time to update, and I have to delay the flow in method1 (await Task.Delay()). I understand that this is a crutch on a crutch, but I do not have enough knowledge to find out how best to proceed here.
Is there a way to change Text.text directly from an asynchronous task?
Or what can you advise in this case? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
CHolfield, 2019-01-20
@CHolfield

add an accessor to the form class
when initializing the form, assign a value to the accessor

public Form1()
        {
            InitializeComponent();
            Instance = this;
        }

and in the task code you do this:
tmpformacc.Invoke((MethodInvoker)delegate
                {
                    tmpformacc.Text.text = СТРОКА; //здесь код, обновляющий GUI
                });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question