Ejemplo n.º 1
0
 public static void QueueOnMainThread(Action action, float time, string name)
 {
     if (!Application.isPlaying)
     {
         return;
     }
     if (Math.Abs(time - 0) > float.Epsilon || !string.IsNullOrEmpty(name))
     {
         lock (Current._delayed)
         {
             DelayedQueueItem existing = null;
             if (!string.IsNullOrEmpty(name))
             {
                 existing = Current._delayed.FirstOrDefault(d => d.name == name);
             }
             if (existing != null)
             {
                 existing.time = Time.time + time;
                 return;
             }
             var queueItem = new DelayedQueueItem();
             queueItem.name   = name;
             queueItem.time   = Time.time + time;
             queueItem.action = action;
             Current._delayed.Add(queueItem);
         }
     }
     else
     {
         lock (Current._actions)
         {
             Current._actions.Add(action);
         }
     }
 }
Ejemplo n.º 2
0
 public static void QueueOnMainThread(Action action, float time, string name)
 {
     if (!Application.isPlaying)
     return;
     if (Math.Abs(time - 0) > float.Epsilon || !string.IsNullOrEmpty(name))
     {
     lock (Current._delayed)
     {
         DelayedQueueItem existing = null;
         if (!string.IsNullOrEmpty(name))
             existing = Current._delayed.FirstOrDefault(d => d.name == name);
         if (existing != null)
         {
             existing.time = Time.time + time;
             return;
         }
         var queueItem = new DelayedQueueItem();
         queueItem.name = name;
         queueItem.time = Time.time + time;
         queueItem.action = action;
         Current._delayed.Add(queueItem);
     }
     }
     else
     {
     lock (Current._actions)
     {
         Current._actions.Add(action);
     }
     }
 }
        public object Queue([FromBody] RequestItem request)
        {
            var item = new DelayedQueueItem(_hub.Clients.All, request.Title, request.Message, request.Delay);

            _queue.Enqueue(item);
            return(new { item.Id });
        }
Ejemplo n.º 4
0
    public static void QueueOnMainThread(Action action, float time, string name)
    {
        if (Math.Abs(time - 0) > float.Epsilon || !string.IsNullOrEmpty(name))
        {
            lock (Instance.delayed)
            {
                DelayedQueueItem existing = null;
                if (!string.IsNullOrEmpty(name))
                    existing = Instance.delayed.FirstOrDefault(d => d.Name == name);
                if (existing != null)
                {
                    existing.Time = Time.time + time;
                    return;
                }
                var queueItem = new DelayedQueueItem();
                queueItem.Name = name;
                queueItem.Time = Time.time + time;
                queueItem.Action = action;
                Instance.delayed.Add(queueItem);
            }
        }
        else
        {
            lock (Instance.actions)
            {
                Instance.actions.Add(action);
            }
        }

    }
Ejemplo n.º 5
0
 public static void QueueOnMainThread(Action action, float time, string name)
 {
     if (Math.Abs(time - 0) > float.Epsilon || !string.IsNullOrEmpty(name))
     {
         lock (Instance.delayed)
         {
             DelayedQueueItem existing = null;
             if (!string.IsNullOrEmpty(name))
             {
                 existing = Instance.delayed.FirstOrDefault(d => d.Name == name);
             }
             if (existing != null)
             {
                 existing.Time = Time.time + time;
                 return;
             }
             var queueItem = new DelayedQueueItem();
             queueItem.Name   = name;
             queueItem.Time   = Time.time + time;
             queueItem.Action = action;
             Instance.delayed.Add(queueItem);
         }
     }
     else
     {
         lock (Instance.actions)
         {
             Instance.actions.Add(action);
         }
     }
 }
Ejemplo n.º 6
0
 // Update is called once per frame
 void Update()
 {
     lock (actions)
     {
         currentActions.Clear();
         currentActions.AddRange(actions);
         actions.Clear();
     }
     for (int i = 0; i < currentActions.Count; ++i)
     {
         Action action = currentActions[i];
         if (action != null)
         {
             action();
         }
     }
     lock (delayed)
     {
         currentDelayed.Clear();
         currentDelayed.AddRange(delayed.Where(d => d.time <= Time.time));
         for (int i = 0; i < currentDelayed.Count; ++i)
         {
             DelayedQueueItem delayedQueneItem = currentDelayed[i];
             delayed.Remove(delayedQueneItem);
         }
     }
     for (int i = 0; i < currentDelayed.Count; ++i)
     {
         DelayedQueueItem delayedQueneItem = currentDelayed[i];
         delayedQueneItem.action();
     }
 }
        protected override async Task ExecuteAsync(CancellationToken token)
        {
            while (!token.IsCancellationRequested)
            {
                var title = await Task.Run(Console.ReadLine, token);

                var item = new DelayedQueueItem(new QueueItemEventHandler(), title, "", 5);
                _queue.Enqueue(item);
                Console.WriteLine($"Item {title} queued with Id: {item.Id}");
            }
        }
Ejemplo n.º 8
0
    /// <summary>
    /// 把执行内容加入到Unity主线和执行
    /// </summary>
    /// <param name="action"></param>
    /// <param name="time"></param>
    public static void QueueOnMainThread(Action action, float time)
    {
        DelayedQueueItem item = new DelayedQueueItem();

        item.Callback = action;
        item.Time     = time;
        lock (lockObj)
        {
            if (time > 0)
            {
                Instance.delayItems.Add(item);
            }
            else
            {
                Instance.addActionList.Add(item);
            }
        }
    }
Ejemplo n.º 9
0
    // Update is called once per frame
    void Update()
    {
        curTime = Time.time;
        if (addActionList.Count > 0 || delayItems.Count > 0)
        {
            lock (lockObj)
            {
                if (addActionList.Count > 0)
                {
                    curActionList.AddRange(addActionList);
                    addActionList.Clear();
                }


                if (delayItems.Count > 0)
                {
                    foreach (DelayedQueueItem delayItem in delayItems)
                    {
                        delayItem.Time += curTime;
                    }
                    curActionList.AddRange(delayItems);
                    delayItems.Clear();
                }
            }
        }


        if (curActionList.Count > 0)
        {
            for (int i = curActionList.Count - 1; i >= 0; i--)
            {
                DelayedQueueItem item = curActionList[i];
                if (item == null || item.Callback == null || curTime < item.Time)
                {
                    continue;
                }
                item.Callback.Invoke();
                curActionList.RemoveAt(i);
            }
        }
    }