private static void HandleAsyncMessage(CommandLineApplication command) { var profileArgument = command.Argument("[profile]", "The name of the json profile file to use (excluded file extension)"); var noProtection = command.Option("--noprotection", "Don't sign or encrypt message", CommandOptionType.NoValue); command.HelpOption("-?|-h|--help"); command.OnExecute(() => { if (string.IsNullOrEmpty(profileArgument.Value)) { command.ShowHelp(); return(2); } Configure(profileArgument.Value, noProtection.HasValue()); if (Directory.Exists(_clientSettings.SourceDirectory) == false) { _logger.LogError("Directory does not exist"); command.ShowHelp(); return(2); } _files = new Stack <string>(Directory.GetFiles(_clientSettings.SourceDirectory)); if (noProtection.HasValue()) { _messagingClient.DefaultMessageProtection = new NoMessageProtection(); } var tasks = new List <Task>(); for (var i = 0; i < _clientSettings.Threads; i++) { tasks.Add(Task.Factory.StartNew(() => { for (var s = GetNextPath(); !string.IsNullOrEmpty(s); s = GetNextPath()) { _logger.LogInformation($"Processing file {s}"); Task.WaitAll(_messagingClient.SendAndContinueAsync(_logger, new OutgoingMessage() { MessageFunction = _clientSettings.MessageFunction, ToHerId = _clientSettings.ToHerId, MessageId = Guid.NewGuid().ToString("D"), ScheduledSendTimeUtc = DateTime.UtcNow, PersonalId = "99999999999", Payload = XDocument.Load(File.OpenRead(s)) })); } })); } tasks.ForEach((w) => w.Wait()); return(0); }); }