123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace EZXR.Glass.Core.Threading
- {
- public class AsyncTask
- {
- public Thread Thread { get; protected set; }
- protected readonly object LockObject = new object();
- private readonly Queue<Action> _callBackQueue;
- private Queue<Action> CallBackQueue
- {
- get
- {
- lock (LockObject)
- {
- return _callBackQueue;
- }
- }
- }
- protected AsyncTask() { }
- public AsyncTask(Action action)
- {
- Thread thread = new Thread(() =>
- {
- action();
- });
- Thread = thread;
- Thread.Start();
- }
- public AsyncTask(Action action, int runFrequencyMs)
- {
- _callBackQueue = new Queue<Action>();
- Thread thread = new Thread(() =>
- {
- while (true)
- {
- action();
- CallBackQueue.Enqueue(action);
- Thread.Sleep(runFrequencyMs);
- }
- });
- Thread = thread;
- Thread.Start();
- }
- public bool IsFinished
- {
- get { return Thread.IsAlive == false; }
- }
- protected virtual bool IsBackgroundTask
- {
- get { return CallBackQueue != null; }
- }
- public void ContinueInMainThread(Action action)
- {
- _onTaskFinished = action;
- }
- private Action _onTaskFinished;
- public virtual void OnTaskFinished()
- {
- if (_onTaskFinished != null)
- {
- if (IsBackgroundTask)
- {
- while (CallBackQueue.Count > 0)
- {
- CallBackQueue.Dequeue();
- _onTaskFinished();
- }
- }
- else
- {
- _onTaskFinished();
- }
- }
- }
- }
- public class AsyncTask<T> : AsyncTask
- {
- public T Result { get; private set; }
- private readonly Queue<T> _backgroundResults;
- private Queue<T> BackgroundResults
- {
- get
- {
- lock (LockObject)
- {
- return _backgroundResults;
- }
- }
- }
- private Action<T> _onTaskFinishedResult;
- protected override bool IsBackgroundTask
- {
- get { return BackgroundResults != null; }
- }
- public AsyncTask(Func<T> func)
- {
- Thread thread = new Thread(() =>
- {
- Result = func();
- });
- Thread = thread;
- Thread.Start();
- }
- public AsyncTask(Func<T> func, int runFrequencyMs)
- {
- _backgroundResults = new Queue<T>();
- Thread thread = new Thread(() =>
- {
- while (true)
- {
- BackgroundResults.Enqueue(func());
- Thread.Sleep(runFrequencyMs);
- }
- });
- Thread = thread;
- Thread.Start();
- }
- public void ContinueInMainThread(Action<T> action)
- {
- _onTaskFinishedResult = action;
- }
- public override void OnTaskFinished()
- {
- if (_onTaskFinishedResult != null)
- {
- if (IsBackgroundTask)
- {
- while (BackgroundResults.Count > 0)
- {
- T result = BackgroundResults.Dequeue();
- _onTaskFinishedResult(result);
- }
- }
- else
- {
- _onTaskFinishedResult(Result);
- }
- }
- else base.OnTaskFinished();
- }
- }
- }
|