Exemplo n.º 1
0
        public static TaskProgress RunBackgroundLongTime(Action <TaskProgress> work)
        {
            var prog   = new TaskProgress();
            var thread = new System.Threading.Thread(state =>
            {
                var progress = state as TaskProgress;
                try
                {
                    work(progress);
                }
#if UNITY_EDITOR
                catch (System.Threading.ThreadAbortException) { }
#endif
                catch (Exception e)
                {
                    LogError(e);
                    progress.Error = e.Message;
                }
                finally
                {
                    progress.Done = true;
                }
            });

            thread.IsBackground = true;
            thread.Start(prog);
            prog.Task = thread;
            return(prog);
        }
Exemplo n.º 2
0
        public static TaskProgress RunBackground(Action <TaskProgress> work)
        {
            var prog = new TaskProgress();

#if NETFX_CORE
            System.Threading.Tasks.Task.Run(() =>
            {
                try
                {
                    work(prog);
                }
                catch (Exception e)
                {
                    LogError(e);
                    prog.Error = e.Message;
                }
                finally
                {
                    prog.Done = true;
                }
            });
#else
            System.Threading.ThreadPool.QueueUserWorkItem(state =>
            {
                try
                {
                    work(prog);
                }
#if UNITY_EDITOR
                catch (System.Threading.ThreadAbortException) { }
#endif
                catch (Exception e)
                {
                    LogError(e);
                    prog.Error = e.Message;
                }
                finally
                {
                    prog.Done = true;
                }
            });
#endif
            return(prog);
        }
Exemplo n.º 3
0
        public static TaskProgress RunBackground(Func <TaskProgress, System.Threading.Tasks.Task> work)
        {
            var prog = new TaskProgress();

            System.Threading.Tasks.Task.Run(async() =>
            {
                try
                {
                    await work(prog);
                }
                catch (Exception e)
                {
                    LogError(e);
                    prog.Error = e.Message;
                }
                finally
                {
                    prog.Done = true;
                }
            });
            return(prog);
        }
Exemplo n.º 4
0
        public static TaskProgress RunBackgroundLongTime(Action <TaskProgress> work)
        {
            var prog = new TaskProgress();
            var task = System.Threading.Tasks.Task.Run(() =>
            {
                try
                {
                    work(prog);
                }
                catch (Exception e)
                {
                    LogError(e);
                    prog.Error = e.Message;
                }
                finally
                {
                    prog.Done = true;
                }
            });

            prog.Task = task;
            return(prog);
        }
Exemplo n.º 5
0
        public static TaskProgress UnzipAsync(string zip, string destdir)
        {
            var prog = new TaskProgress();

            try
            {
                if (IsFileExist(zip))
                {
                    var taskcnt    = Environment.ProcessorCount;
                    int entryIndex = 0;
                    int finishCnt  = 0;
                    Action <TaskProgress> UnzipWork = p =>
                    {
                        try
                        {
                            using (var stream = PlatDependant.OpenRead(zip))
                            {
                                using (var zipa = new ZipArchive(stream, ZipArchiveMode.Read))
                                {
                                    var entries = zipa.Entries;
                                    if (entries != null)
                                    {
                                        var index = System.Threading.Interlocked.Increment(ref entryIndex) - 1;
                                        if (index == 0)
                                        {
                                            prog.Total = entries.Count;
                                        }
                                        while (index < entries.Count)
                                        {
                                            System.Threading.Interlocked.Increment(ref prog.Length);

                                            try
                                            {
                                                var entry = entries[index];
                                                var name  = entry.FullName;
                                                var dest  = System.IO.Path.Combine(destdir, name);
                                                using (var srcs = entry.Open())
                                                {
                                                    using (var dsts = PlatDependant.OpenWrite(dest))
                                                    {
                                                        srcs.CopyTo(dsts);
                                                    }
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                LogError(e);
                                            }

                                            index = System.Threading.Interlocked.Increment(ref entryIndex) - 1;
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            LogError(e);
                            prog.Error = e.Message;
                        }
                        finally
                        {
                            if (System.Threading.Interlocked.Increment(ref finishCnt) >= taskcnt)
                            {
                                prog.Done = true;
                            }
                        }
                    };
                    for (int i = 0; i < taskcnt; ++i)
                    {
                        RunBackground(UnzipWork);
                    }
                    return(prog);
                }
            }
            catch (Exception e)
            {
                LogError(e);
                prog.Error = e.Message;
            }
            prog.Done = true;
            return(prog);
        }