Exemplo n.º 1
0
        public async void Execute(string idtask, CancellationTokenSource cancellationToken)
        {
            var task = GetTaskById(idtask);

            if (task != null)
            {
                LogIts log;
                try
                {
                    var lst = SerializeIts.DeserializeObject <List <TaskParamIts> >(task.Tarefa);
                    //inicia
                    StartTask(task.IdTask, cancellationToken);

                    InsertLogTask(new LogIts(task.IdTask, "Obtendo o tipo..."));
                    // Execute the task
                    //var cl = Assembly.LoadFile(@"C:\Users\Gercy\Documents\TFS_ITsolution\ITE\ITE.Entidades\bin\Debug\ITE.Entidades.dll");
                    //var qname = typeof(cl.Assembly);
                    var typeName = Type.GetType(task.Classe);

                    object instance = Activator.CreateInstance(typeName, task.IdTask);

                    InsertLogTask(new LogIts(task.IdTask, "Classe: " + task.Classe));
                    InsertLogTask(new LogIts(task.IdTask, "Metodo: " + task.Metodo));

                    var methodInfo = typeName.GetMethod(task.Metodo);
                    var param      = methodInfo.GetParameters();
                    int i          = 0;

                    List <TaskParamIts> parametrosTask = SerializeIts.DeserializeObject <List <TaskParamIts> >(task.Tarefa);
                    dynamic[]           parmOrigs22    = new dynamic[parametrosTask.Count];

                    InsertLogTask(new LogIts(task.IdTask, "Parametros:"));


                    foreach (var item in parametrosTask)
                    {
                        if (item.ParameterValue.ToString() == "System.Threading.CancellationTokenSource")
                        {
                            parmOrigs22[i] = cancellationToken;
                        }
                        else
                        {
                            log = new LogIts(task.IdTask, Thread.CurrentThread.ManagedThreadId.ToString(),
                                             item.ParameterName.ToString()
                                             + ": " + item.ParameterValue.ToString(), String.Empty);

                            //insere log
                            InsertLogTask(log);
                            var stp = item.ParameterType.AssemblyQualifiedName;
                            var tp  = Type.GetType(stp);
                            parmOrigs22[i] = item.ParameterValue;
                            //parmOrigs22[i] = Convert.ChangeType(item.ParameterValue,
                            //     tp);
                        }
                        i++;
                    }
                    //object[] parmOrigs = new object[] { 1000, task.IdTask, cts };
                    try
                    {
                        Task t1 = new Task(() =>
                        {
                            methodInfo.Invoke(instance, parmOrigs22);
                        });

                        await Task.Factory.StartNew(() => t1.Start());

                        Task ok = await Task.WhenAny(t1);

                        if ((ok.IsCompleted && ok.Exception == null) && !cancellationToken.IsCancellationRequested)
                        {
                            //Work task
                            DoWorkTask(task.IdTask);
                        }
                        if (ok.Exception != null)
                        {
                            var msg = ok.Exception.InnerExceptions.First();

                            throw new Exception(msg.InnerException.Message);
                        }

                        if (cancellationToken.IsCancellationRequested)
                        {
                            throw new OperationCanceledException("Operação cancelada.");
                        }
                    }
                    catch (OperationCanceledException exCancel)
                    {
                        log = new LogIts(task.IdTask,
                                         Thread.CurrentThread.ManagedThreadId.ToString(),
                                         exCancel.Message
                                         , "Operacao cancelada");
                        InsertLogTask(log);
                        SuspendTask(task.IdTask);
                    }
                    catch (TargetInvocationException tex)
                    {
                        log = new LogIts(task.IdTask,
                                         Thread.CurrentThread.ManagedThreadId.ToString(),
                                         tex.Message
                                         , tex.Message);
                        InsertLogTask(log);
                        StoppedTaskException(task.IdTask);
                    }
                    catch (Exception ex)
                    {
                        log = new LogIts(task.IdTask,
                                         Thread.CurrentThread.ManagedThreadId.ToString(),
                                         ex.Message
                                         , ex.Message);
                        InsertLogTask(log);
                        StoppedTaskException(task.IdTask);
                    }
                }//fim try
                catch (Exception ex)
                {
                    log = new LogIts(task.IdTask,
                                     Thread.CurrentThread.ManagedThreadId.ToString(),
                                     ex.Message
                                     , ex.Message);
                    InsertLogTask(log);
                    StoppedTaskException(task.IdTask);
                }
            }
        }
Exemplo n.º 2
0
        private Task InsertLogTask(LogIts log)
        {
            var ctx = SchedulerContextGeneric <LogIts> .Instance;

            return(ctx.Dao.SaveAsync(log));
        }