Esempio n. 1
0
        private async Task ActionTask(CancellationToken token)
        {
            try
            {
                await OnActionAsync(_token).ConfigureAwait(false);

                if (EndAfterTaskFinish)
                {
                    ServiceContainer.ServiceExit();
                }
            }
            catch (Exception ex)
            {
                Core.Log.Write(ex);
                ServiceContainer.ServiceExit();
            }
        }
Esempio n. 2
0
 public void OnStart(string[] args)
 {
     try
     {
         Core.Log.InfoBasic("Starting service");
         _tokenSource = new CancellationTokenSource();
         _token       = _tokenSource.Token;
         _task        = _creationFunction(_token);
         if (_task is null)
         {
             throw new NullReferenceException("The returned task from the Task creation function can't be null");
         }
         _task.ContinueWith(t =>
         {
             if (t.Exception != null)
             {
                 Core.Log.Write(t.Exception);
                 ServiceContainer.ServiceExit();
             }
             if (EndAfterTaskFinish)
             {
                 ServiceContainer.ServiceExit();
             }
         }, TaskContinuationOptions.ExecuteSynchronously);
         if (!_task.IsCompleted && _task.Status != TaskStatus.Running && _task.Status != TaskStatus.WaitingForActivation && _task.Status != TaskStatus.WaitingForChildrenToComplete && _task.Status != TaskStatus.WaitingToRun)
         {
             _task.Start();
         }
         Core.Log.InfoBasic("Service started");
         if (EndAfterTaskFinish)
         {
             _task.WaitAsync();
         }
     }
     catch (Exception ex)
     {
         Core.Log.Write(ex);
         throw;
     }
 }
Esempio n. 3
0
 public void OnStart(string[] args)
 {
     try
     {
         Core.Log.InfoBasic("Starting service");
         StartArguments = args;
         _tokenSource   = new CancellationTokenSource();
         _token         = _tokenSource.Token;
         _task          = Task.Factory.StartNew(() =>
         {
             try
             {
                 OnAction(_token);
                 if (EndAfterTaskFinish)
                 {
                     ServiceContainer.ServiceExit();
                 }
             }
             catch (Exception ex)
             {
                 Core.Log.Write(ex);
                 ServiceContainer.ServiceExit();
             }
         }, _token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
         Core.Log.InfoBasic("Service started");
         if (EndAfterTaskFinish)
         {
             _task.WaitAsync();
         }
     }
     catch (Exception ex)
     {
         Core.Log.Write(ex);
         throw;
     }
 }