Beispiel #1
0
        public static TemporaryTask AddTempTask <T>(string taskName, IEnumerable <T> enumable, Action <T> action,
                                                    Action <int> contineAction = null, int count = -1, bool autoStart = true, int notifyInterval = 1,
                                                    Func <int> delayFunc       = null)
        {
            var tempTask = new TemporaryTask {
                Name = taskName
            };
            // start a task with a means to do a hard abort (unsafe!)

            var index = 0;

            if (notifyInterval <= 0)
            {
                notifyInterval = 1;
            }
            tempTask.TaskAction = () =>
            {
                if (enumable is ICollection <T> )
                {
                    count = ((ICollection <T>)enumable).Count;
                }

                foreach (var r in enumable)
                {
                    action?.Invoke(r);

                    if (r is int)
                    {
                        index = Convert.ToInt32(r);
                    }
                    else
                    {
                        index++;
                    }

                    if (index % notifyInterval != 0)
                    {
                        continue;
                    }
                    if (tempTask.CheckCancel())
                    {
                        tempTask.WasCanceled = true;
                        break;
                    }
                    if (delayFunc != null)
                    {
                        Thread.Sleep(delayFunc());
                    }
                    if (count > 0)
                    {
                        tempTask.Percent = tempTask.CurrentIndex * 100 / count;
                    }
                    tempTask.CurrentIndex = index;
                    tempTask.CheckWait();
                }
                if (!tempTask.WasCanceled)
                {
                    XLogSys.Print.Debug($"任务【{tempTask.Name}】已经成功完成");
                    tempTask.Percent = 100;
                }
                if (contineAction != null)
                {
                    ControlExtended.UIInvoke(() => contineAction(tempTask.CurrentIndex));
                }
            };

            if (autoStart)
            {
                tempTask.Start();
            }
            return(tempTask);
        }
Beispiel #2
0
        public static TemporaryTask <T> AddTempTask <T>(TemporaryTask <T> tempTask, IEnumerable <T> source,
                                                        Func <T, IEnumerable <T> > func,
                                                        Action <int> continueAction = null, int count = -1, bool autoStart = true, int notifyInterval = 1,
                                                        Func <int> delayFunc        = null) where T : IDictionarySerializable

        {
            if (notifyInterval <= 0)
            {
                notifyInterval = 1;
            }
            tempTask.Total      = count;
            tempTask.TaskAction = () =>
            {
                if (source is ICollection <T> )
                {
                    count = ((ICollection <T>)source).Count;
                }
                tempTask.CheckWait();
                foreach (var r in source)
                {
                    if (tempTask.CheckCancel())
                    {
                        tempTask.WasCanceled = true;
                        break;
                    }
                    foreach (var item in func != null ? func(r) : new List <T> {
                        r
                    })
                    {
                        tempTask.CheckWait();

                        if (tempTask.CheckCancel())
                        {
                            tempTask.WasCanceled = true;
                            break;
                        }
                        if (tempTask.OutputIndex % notifyInterval != 0)
                        {
                            continue;
                        }
                        tempTask.OutputIndex++;
                    }
                    if (delayFunc != null)
                    {
                        Thread.Sleep(delayFunc());
                    }
                    if (tempTask.OutputIndex % notifyInterval != 0)
                    {
                        continue;
                    }
                    if (count > 0)
                    {
                        tempTask.Percent = tempTask.OutputIndex * 100 / count;
                    }
                }
                if (!tempTask.WasCanceled)
                {
                    XLogSys.Print.Debug(string.Format(GlobalHelper.Get("key_338"), tempTask.Name));
                    tempTask.Percent = 100;
                }
                if (continueAction != null)
                {
                    ControlExtended.UIInvoke(() => continueAction(tempTask.OutputIndex));
                }
            };

            if (autoStart)
            {
                tempTask.Start();
            }
            return(tempTask);
        }
Beispiel #3
0
        public static TemporaryTask AddTempTask <T>(string taskName, IEnumerable <T> enumable, Action <T> action,
                                                    Action <int> contineAction = null, int count = -1, bool autoStart = true, int notifyInterval = 1, Func <int> delayFunc = null)
        {
            var tempTask = new TemporaryTask {
                Name = taskName
            };

            var index = 0;

            if (notifyInterval <= 0)
            {
                notifyInterval = 1;
            }
            tempTask.TaskAction = () =>
            {
                if (enumable is ICollection <T> )
                {
                    count = ((ICollection <T>)enumable).Count;
                }

                var finish = true;
                foreach (var r in enumable)
                {
                    action?.Invoke(r);

                    if (r is int)
                    {
                        index = Convert.ToInt32(r);
                    }
                    else
                    {
                        index++;
                    }

                    if (index % notifyInterval != 0)
                    {
                        continue;
                    }
                    if (tempTask.CheckCancel())
                    {
                        finish = false;
                        break;
                    }
                    if (delayFunc != null)
                    {
                        Thread.Sleep(delayFunc());
                    }
                    if (count > 0)
                    {
                        tempTask.Percent = tempTask.CurrentIndex * 100 / count;
                    }
                    tempTask.CurrentIndex = index;
                    tempTask.CheckWait();
                }
                if (finish)
                {
                    tempTask.Percent = 100;
                }
                if (contineAction != null)
                {
                    ControlExtended.UIInvoke(() => contineAction(tempTask.CurrentIndex));
                }
            };

            if (autoStart)
            {
                tempTask.Start();
            }
            return(tempTask);
        }