Exemplo n.º 1
0
        /// <summary>
        /// Starts mail controller.
        /// </summary>
        /// <param name="serverType"></param>
        /// <param name="serverEncryption"></param>
        /// <param name="host"></param>
        /// <param name="user"></param>
        /// <param name="password"></param>
        public void Start(MailServerType serverType,
                          MailServerEncryption serverEncryption,
                          string host,
                          string user,
                          string password)
        {
            // Prevents possible issues when Start was called more then once.
            if (MainLoop != null && !MainLoop.IsCompleted)
            {
                Logger.Error("Unexpected engine start detected.");
                return;
                // At the moment it logs error message instead of throwing it.
                // throw new Exception("Unexpected engine start detected.");
            }

            // Save all settings to local variable.
            _host             = !string.IsNullOrWhiteSpace(host) ? host : throw new ArgumentException("Invalid host.");
            _user             = !string.IsNullOrWhiteSpace(user) ? user : throw new ArgumentException("Invalid user.");
            _password         = !string.IsNullOrWhiteSpace(password) ? password : throw new ArgumentException("Invalid password.");
            _serverType       = serverType;
            _serverEncryption = serverEncryption;

            MainLoop = Task.Factory.StartNew(() => StartMailCheckAsyncLoop(_cancelSource.Token), _cancelSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
            MainLoop.ContinueWith(e =>
            {
                Logger.Error(e.Exception, "StartNewOnFaulted");
            }, TaskContinuationOptions.OnlyOnFaulted);
            MainLoop.ContinueWith(e =>
            {
                Logger.Trace(e.Exception, "StartNewOnCanceled");
            }, TaskContinuationOptions.OnlyOnCanceled);

            Logger.Trace($"Started");
        }