|
- using System;
- using System.Collections.Generic;
- using System.Threading;
- using CIG.Extensions;
- using UnityEngine;
-
- namespace CIG
- {
- public class AdProviderMonoBehaviour : MonoBehaviour
- {
- private void Start()
- {
- this._mainThread = Thread.CurrentThread;
- this.InvokeRepeating(new Action(this.InvokeMainThreadQueue), 1f, 1f, true);
- }
-
- private void OnDestroy()
- {
- this.CancelInvoke(new Action(this.InvokeMainThreadQueue));
- }
-
- public void InvokeOnMainThread(Action action, float time)
- {
- if (object.ReferenceEquals(Thread.CurrentThread, this._mainThread))
- {
- this.Invoke(action, time, false);
- }
- else
- {
- this.EnqueueActionOnMainThread(delegate
- {
- this.Invoke(action, time, false);
- });
- }
- }
-
- private void EnqueueActionOnMainThread(Action action)
- {
- object updateOnMainThreadActionsLock = this._updateOnMainThreadActionsLock;
- lock (updateOnMainThreadActionsLock)
- {
- this._updateOnMainThreadActions.Enqueue(action);
- }
- }
-
- private void InvokeMainThreadQueue()
- {
- if (this._updateOnMainThreadActions.Count > 0)
- {
- Action action;
- do
- {
- action = null;
- object updateOnMainThreadActionsLock = this._updateOnMainThreadActionsLock;
- lock (updateOnMainThreadActionsLock)
- {
- if (this._updateOnMainThreadActions.Count > 0)
- {
- action = this._updateOnMainThreadActions.Dequeue();
- }
- }
- if (action != null)
- {
- action();
- }
- }
- while (action != null);
- }
- }
-
- private Thread _mainThread;
-
- private readonly object _updateOnMainThreadActionsLock = new object();
-
- private readonly Queue<Action> _updateOnMainThreadActions = new Queue<Action>();
- }
- }
|