예제 #1
0
        /*初始化一个独立的线程*/
        public void StartALongTask <T>(Action <T> action, T obj = default(T), string threadName = "", Action <string> errCode = null)
        {
            TaskItem <T> item = new TaskItem <T>(GetThreadName());

            item.action    = action;
            item.parameter = obj;
            item.cusName   = threadName;

            lock (dic)
            {
                dic.Add(item.id, item);
            }

            Action ac = () =>
            {
                try
                {
                    item.action(obj);
                }
                catch (Exception e)
                {
                    lock (dic)
                    {
                        item.SetErrCode(e.ToString());
                    }
                }
                item.Dispose();
                lock (dic)
                    dic.Remove(item.id);
            };

            Task.Run(ac);
        }
        public void InitializeAsync(CancellationTokenSource cancellationTokenSource,
                                    ITaskManager taskManager,
                                    IHttpDownloaderContainer httpDownloaderContainer,
                                    IErrorFeedback errorFeedback,
                                    IProxySearchFeedback proxySearchFeedback,
                                    IGeoIP geoIP,
                                    IRatingManager ratingManager)
        {
            this.httpDownloaderContainer = httpDownloaderContainer;
            TaskItem taskItem = taskManager.Create(Resources.ConfigureProviderOfProxyDetails);

            taskItem.UpdateDetails(Resources.DetermineCurrentIPAddress);

            initializatinTask = httpDownloaderContainer.HttpDownloader
                                .GetContentOrNull(MyIPUrl, null, cancellationTokenSource)
                                .ContinueWith(task =>
            {
                try
                {
                    if (task.Result != null)
                    {
                        IPAddress.TryParse(task.Result.Trim(), out myIPAddress);
                    }
                }
                finally
                {
                    taskItem.Dispose();
                }
            });
        }
        private System.Collections.Generic.IEnumerator <byte> LoadResource_INTERNAL <T>(ResourceBase resource, IResourceReference component, string customResourcePath, System.Action <T> callback, bool async)      /*where T : Object*/
        {
            if (resource.HasDirectRef() == true)
            {
                callback.Invoke(resource.GetDirectRef <T>());
                yield break;
            }

            if (this.forceAsyncOff == true)
            {
                // Disabling async resource loading on Android, because for some reason Resources.LoadAsync() is terribly slow
                // as of Unity 5.6.2p2 (takes ~2 minutes to load some resources).
                async = false;
            }

            var task = new TaskItem();

            task.id                 = resource.GetId();
            task.async              = async;
            task.resource           = resource;
            task.component          = component;
            task.customResourcePath = customResourcePath;
            task.onSuccess          = (obj) => {
                T resultObj = default(T);

                if (obj is GameObject && typeof(T).HasBaseType(typeof(Component)) == true && typeof(T).HasBaseType(typeof(GameObject)) == false)
                {
                    resultObj = (obj as GameObject).GetComponent <T>();
                }
                else
                {
                    //if (UnityEngine.UI.Windows.Constants.LOGS_ENABLED == true) UnityEngine.Debug.Log(typeof(T) + " << " + obj);
                    resultObj = (T)obj;
                }

                callback.Invoke(resultObj);
            };
            task.onFailed = () => {
                callback.Invoke(default(T));
            };

                        #if UNITY_EDITOR
            this.tasks.Add(task);
                        #endif

            //if (UnityEngine.UI.Windows.Constants.LOGS_ENABLED == true) UnityEngine.Debug.Log("Resource Task: " + task.resource.assetPath);

            var coroutine = this.StartTask <T>(task);
            while (coroutine.MoveNext() == true)
            {
                yield return(0);
            }

            task.Dispose();

                        #if UNITY_EDITOR
            this.tasks.RemoveAll(x => x.id == task.id);
                        #endif
        }
예제 #4
0
        private static void RunAction(object param)
        {
            TaskItem item = (TaskItem)param;

            try
            {
                item.Execute();
                item.Dispose();
            }
            catch
            {
            }
            finally
            {
                item.Dispose();
                Interlocked.Decrement(ref numThreads);
            }
        }
        private IEnumerator LoadResource_INTERNAL <T>(ResourceBase resource, IImageComponent component, Graphic graphic, string customResourcePath, System.Action <T> callback) where T : Object
        {
            if (this.taskInterations == null)
            {
                this.taskInterations = new Dictionary <Graphic, int>();
            }

            var iteration = 0;

            if (graphic != null)
            {
                if (this.taskInterations.TryGetValue(graphic, out iteration) == false)
                {
                    this.taskInterations.Add(graphic, ++iteration);
                }
            }

            var task = new TaskItem();

            task.id                 = resource.GetId();
            task.resource           = resource;
            task.component          = component;
            task.graphic            = graphic;
            task.customResourcePath = customResourcePath;
            task.onSuccess          = (obj) => {
                if (graphic != null)
                {
                    var iterationFailed = !(graphic == null || iteration == this.taskInterations[graphic]);
                    if (iterationFailed == false)
                    {
                        callback.Invoke(obj as T);
                    }
                    else
                    {
                        task.onFailed.Invoke();
                    }
                }
                else
                {
                    callback.Invoke(obj as T);
                }
            };
            task.onFailed = () => {
                callback.Invoke(null);
            };

            this.tasks.Add(task);

            //Debug.Log("Resource Task: " + task.resource.assetPath);

            yield return(this.StartCoroutine(this.StartTask <T>(task)));

            task.Dispose();
            this.tasks.Remove(task);
        }
예제 #6
0
        private System.Collections.Generic.IEnumerator <byte> LoadResource_INTERNAL <T>(ResourceBase resource, IResourceReference component, string customResourcePath, System.Action <T> callback, bool async)      /*where T : Object*/
        {
            var task = new TaskItem();

            task.id                 = resource.GetId();
            task.async              = async;
            task.resource           = resource;
            task.component          = component;
            task.customResourcePath = customResourcePath;
            task.onSuccess          = (obj) => {
                T resultObj = default(T);

                if (obj is GameObject && typeof(T).HasBaseType(typeof(Component)) == true && typeof(T).HasBaseType(typeof(GameObject)) == false)
                {
                    resultObj = (obj as GameObject).GetComponent <T>();
                }
                else
                {
                    //Debug.Log(typeof(T) + " << " + obj);
                    resultObj = (T)obj;
                }

                callback.Invoke(resultObj);
            };
            task.onFailed = () => {
                callback.Invoke(default(T));
            };

            this.tasks.Add(task);

            //Debug.Log("Resource Task: " + task.resource.assetPath);

            task.coroutine = this.StartTask <T>(task);
            while (task.coroutine.MoveNext() == true)
            {
                yield return(0);
            }

            task.Dispose();
            this.tasks.Remove(task);
        }
예제 #7
0
        public override void InitializeAsync(CancellationTokenSource cancellationTokenSource,
                                             ITaskManager taskManager,
                                             IHttpDownloaderContainer httpDownloaderContainer,
                                             IErrorFeedback errorFeedback,
                                             IProxySearchFeedback proxySearchFeedback,
                                             IGeoIP geoIP,
                                             IRatingManager ratingManager)
        {
            base.InitializeAsync(cancellationTokenSource, taskManager, httpDownloaderContainer, errorFeedback, proxySearchFeedback, geoIP, ratingManager);

            TaskItem taskItem = taskManager.Create(Resources.ConfiguringProxyChecker);

            try
            {
                taskItem.UpdateDetails(string.Format(Resources.DownloadingFormat, Url));

                initializatinTask = httpDownloaderContainer.HttpDownloader.GetContentOrNull(Url, null, cancellationTokenSource)
                                    .ContinueWith(task =>
                {
                    try
                    {
                        if (task.Result == null)
                        {
                            errorFeedback.SetException(new InvalidOperationException(string.Format(Resources.CannotDownloadContent, Url)));
                            cancellationTokenSource.Cancel();
                        }
                        else
                        {
                            analyzedText = AnalyzeText(task.Result);
                        }
                    }
                    finally
                    {
                        taskItem.Dispose();
                    }
                });
            }
            catch (TaskCanceledException)
            {
            }
        }