队列先进后出,栈先进先出,解决简单的异步并发问题.栈辅助类非原创
一.队列辅助类
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
/// <summary>
/// 异步任务队列
/// </summary>
public class AsyncQueueTask : MonoBehaviour
{
public delegate Task<bool> TaskDel(object data);
private bool isWait;
//方法队列
Queue<TaskDel> taskQueue = new Queue<TaskDel>();
//参数队列
Queue<object> paraQueue = new Queue<object>();
/// <summary>
/// 调用这个即可
/// </summary>
/// <param name="ac">方法</param>
/// <param name="data">参数</param>
public void AddQueueTask(TaskDel ac, object data)
{
Debug.Log("入队");
taskQueue.Enqueue(ac);
paraQueue.Enqueue(data);
StartTask();
}
async void StartTask()
{
if (!isWait && taskQueue.Count != 0)
{
isWait = true;
await taskQueue.Peek()(paraQueue.Peek());
Debug.Log("出队");
taskQueue.Dequeue();
paraQueue.Dequeue();
isWait = false;
StartTask();
}
}
}
二.队列Samples
每次点击不会干扰之前异步的代码运行
执行完当前异步方法后在执行之后的方法
呈现先进后出的效果,直到执行完队列中的所有方法
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Rendering;
public class QueueTaskSamples : MonoBehaviour
{
private int _clickCount;
public AsyncQueueTask _asyncQueue;
/// <summary>
///按钮绑定方法
/// </summary>
public void OnClick()
{
_clickCount++;
_asyncQueue.AddQueueTask(OnMethod,_clickCount);
}
/// <summary>
/// 绑定方法实体
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
private async Task<bool> OnMethod(object index)
{
await Task.Delay(1000);
Debug.Log("你点击了按钮"+index.ToString()+"次");
return true;
}
}
三.栈辅助类(基于队列进行修改)
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
public class AsyncStackTask : MonoBehaviour
{
public delegate Task<bool> TaskDel(object data);
private bool isWait;
//方法堆栈
Stack<TaskDel> taskStack = new Stack<TaskDel>();
//参数堆栈
Stack<object> paraStack = new Stack<object>();
/// <summary>
/// 调用这个即可
/// </summary>
/// <param name="ac">方法</param>
/// <param name="data">参数</param>
public void AddStackTask(TaskDel ac, object data)
{
Debug.Log("入栈");
taskStack.Push(ac);
paraStack.Push(data);
StartTask();
}
async void StartTask()
{
if (!isWait && taskStack.Count != 0)
{
isWait = true;
await taskStack.Peek()(paraStack.Peek());
Debug.Log("出栈");
taskStack.Pop();
paraStack.Pop();
isWait = false;
StartTask();
}
}
}
四.栈Samples
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Scirpts;
using UnityEngine;
using UnityEngine.Rendering;
public class QueueTaskSamples : MonoBehaviour
{
private int _clickCount;
public AsyncQueueTask _asyncQueue;
public AsyncStackTask _asyncStack;
/// <summary>
///按钮绑定方法
/// </summary>
public void OnClick()
{
_clickCount++;
_asyncStack.AddStackTask(OnMethod,_clickCount);
// _asyncQueue.AddQueueTask(OnMethod,_clickCount);
}
/// <summary>
/// 队列绑定方法实体
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
private async Task<bool> OnMethod(object index)
{
await Task.Delay(1000);
Debug.Log("你点击了按钮"+index.ToString()+"次");
return true;
}
}
使用效果图
|