Skip to content

Commit

Permalink
Merge pull request #190 from maifeeulasad/stuck-fix
Browse files Browse the repository at this point in the history
solution to make network call on main thread
  • Loading branch information
jdnichollsc authored Dec 9, 2021
2 parents 8719ac6 + 549d846 commit f5430ef
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions Proyecto26.RestClient/Helpers/ExecuteOnMainThread.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using UnityEngine;

namespace Proyecto26.Helper {

/// <summary>
/// Original solution from: https://stackoverflow.com/a/53818416/10305444
/// Should also read: https://gamedev.stackexchange.com/a/195298/126092
/// Usage:
/// ```
/// ExecuteOnMainThread.RunOnMainThread.Enqueue(() => {
/// Code here will be called in the main thread...
/// });
/// ```
/// </summary>
public class ExecuteOnMainThread: MonoBehaviour {

/// <summary>
/// Store all instance of Action's and try to invoke them
/// </summary>
public static readonly ConcurrentQueue < Action > RunOnMainThread = new ConcurrentQueue < Action > ();

/// <summary>
/// Unity's Update method
/// </summary>
void Update() {
if (!RunOnMainThread.IsEmpty) {
while (RunOnMainThread.TryDequeue(out var action)) {
action?.Invoke();
}
}
}
}
}

0 comments on commit f5430ef

Please sign in to comment.