public List <String> GetAvaliableProcesses() { var output = new ConcurrentStack <Process>(); Parallel.ForEach(Process.GetProcesses(), (proc) => { try { Parallel.For(0, proc.Modules.Count, (i, loopState) => { var moduleName = proc.Modules[i].ModuleName; if (Desktop40LanguageRuntime().Contains(moduleName))// || OldDesktopLanguageRuntime().Contains(moduleName)) { output.Push(proc); loopState.Break(); } }); } catch (Win32Exception ex) // access is denied {} }); return(output .OrderBy(x => x.ProcessName) .Select(x => x.ProcessName) .ToList()); }
private static void ImportDataFromCSV(string exportPath, string exportFileName, string headerText, ConcurrentStack <Person> csvFileList) { try { var targetPath = Path.Combine(Environment.CurrentDirectory, exportPath); var fullFilePath = Path.Combine(targetPath, exportFileName) + ".csv"; if (File.Exists(fullFilePath)) { if (new FileInfo(fullFilePath).Length == 0) { Console.WriteLine(string.Format("Target file: {0} without content inside(is empty)", fullFilePath)); return; } headerText = headerText.Replace(',', '|'); Console.WriteLine($"|{headerText}|"); var sortList = csvFileList.OrderBy(p => p.ID); var allLines = string.Join(Environment.NewLine, sortList.Select(s => s.ToOutputString())); Console.WriteLine(allLines); } else { Console.WriteLine(string.Format("Target file: {0} not existed", fullFilePath)); } } catch (Exception ex) { Console.WriteLine(string.Format("Error message: {0}", "StackTrace: {1}"), ex.Message, ex.StackTrace); } }
public async Task <IEnumerable <ITransaction> > GetAsync(IEnumerable <string> ids) { var result = new ConcurrentStack <ITransaction>(); var idList = ids as IList <string> ?? ids.ToList(); var loadTransactionTasks = idList.Select(id => GetAsync(id).ContinueWith(task => { if (task.Result != null) { result.Push(task.Result); } })); await Task.WhenAll(loadTransactionTasks); return(result.OrderBy(p => idList.IndexOf(p.TransactionId))); }
public async Task AllSenderEvents() { var actualEvents = new ConcurrentStack <string>(); var expectedEvents = new ConcurrentStack <string>(); var mms = new MailMergeSender { Config = _settings.SenderConfig }; mms.Config.MaxNumOfSmtpClients = 1; // Event raising before merging starts mms.OnMergeBegin += (mailMergeSender, mergeBeginArgs) => { actualEvents.Push(nameof(mms.OnMergeBegin)); }; // Event raising when getting the merged MimeMessage of the MailMergeMessage has failed. mms.OnMessageFailure += (mailMergeSender, messageFailureArgs) => { actualEvents.Push(nameof(mms.OnMessageFailure)); }; // Event raising before sending a single mail message starts mms.OnBeforeSend += (smtpClient, beforeSendArgs) => { }; // Event raising right after the SmtpClient's connection to the server is up (but not yet authenticated). mms.OnSmtpConnected += (smtpClient, smtpClientArgs) => { actualEvents.Push(nameof(mms.OnSmtpConnected)); }; // Event raising after the SmtpClient has authenticated on the server. mms.OnSmtpAuthenticated += (smtpClient, smtpClientArgs) => { actualEvents.Push(nameof(mms.OnSmtpAuthenticated)); }; // Event raising after the SmtpClient has disconnected from the SMTP mail server. mms.OnSmtpDisconnected += (smtpClient, smtpClientArgs) => { actualEvents.Push(nameof(mms.OnSmtpDisconnected)); }; // Event raising if sending a single mail message fails mms.OnSendFailure += (smtpClient, sendFailureArgs) => { actualEvents.Push(nameof(mms.OnSendFailure)); }; // Event raising before sending a single mail message is finished mms.OnAfterSend += (smtpClient, afterSendArgs) => { actualEvents.Push(nameof(mms.OnAfterSend)); }; // Event raising each time before and after a single message was sent mms.OnMergeProgress += (mailMergeSender, progressArgs) => { actualEvents.Push(nameof(mms.OnMergeProgress)); }; // Event raising after merging is completed mms.OnMergeComplete += (mailMergeSender, completedArgs) => { actualEvents.Push(nameof(mms.OnMergeComplete)); }; var recipients = new List <Recipient>(); for (var i = 0; i < 1; i++) { recipients.Add(new Recipient() { Email = $"recipient-{i}@example.com", Name = $"Name of {i}" }); } var mmm = new MailMergeMessage("Event tests", "This is the plain text part for {Name} ({Email})") { Config = _settings.MessageConfig }; mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{Name}", "{Email}")); var sequenceOfExpectedEvents = new[] { nameof(mms.OnMergeBegin), nameof(mms.OnMergeProgress), nameof(mms.OnSmtpConnected), nameof(mms.OnAfterSend), nameof(mms.OnMergeProgress), nameof(mms.OnMergeComplete), nameof(mms.OnSmtpDisconnected) }; #region * Synchronous send method * mms.Send(mmm, recipients); expectedEvents.Clear(); expectedEvents.PushRange(sequenceOfExpectedEvents); Assert.AreEqual(expectedEvents.Count, actualEvents.Count); // sequence of sync sending is predefined for (var i = 0; i < actualEvents.Count; i++) { expectedEvents.TryPop(out string expected); actualEvents.TryPop(out string actual); Assert.AreEqual(expected, actual); } #endregion #region * Async send method * actualEvents.Clear(); expectedEvents.Clear(); expectedEvents.PushRange(sequenceOfExpectedEvents); await mms.SendAsync(mmm, recipients); Assert.AreEqual(expectedEvents.Count, actualEvents.Count); // sequence of async sending may be different from sync, but all events must exists var sortedActual = actualEvents.OrderBy(e => e).ToArray(); var sortedExpected = expectedEvents.OrderBy(e => e).ToArray(); for (var i = 0; i < sortedActual.Length; i++) { Assert.AreEqual(sortedExpected[i], sortedActual[i]); } #endregion }
public async Task AllSenderEventsSingleMail(string somePlaceholder, bool withParseFailure) { #region * Sync and Async preparation * var actualEvents = new ConcurrentStack <string>(); var expectedEvents = new ConcurrentStack <string>(); var mms = new MailMergeSender { Config = _settings.SenderConfig }; mms.Config.MaxNumOfSmtpClients = 1; // Event raising when getting the merged MimeMessage of the MailMergeMessage has failed. mms.OnMessageFailure += (mailMergeSender, messageFailureArgs) => { actualEvents.Push(nameof(mms.OnMessageFailure)); }; // Event raising before sending a single mail message starts mms.OnBeforeSend += (smtpClient, beforeSendArgs) => { actualEvents.Push(nameof(mms.OnBeforeSend)); }; // Event raising right after the SmtpClient's connection to the server is up (but not yet authenticated). mms.OnSmtpConnected += (smtpClient, smtpClientArgs) => { actualEvents.Push(nameof(mms.OnSmtpConnected)); }; // Event raising after the SmtpClient has authenticated on the server. mms.OnSmtpAuthenticated += (smtpClient, smtpClientArgs) => { actualEvents.Push(nameof(mms.OnSmtpAuthenticated)); }; // Event raising after the SmtpClient has disconnected from the SMTP mail server. mms.OnSmtpDisconnected += (smtpClient, smtpClientArgs) => { actualEvents.Push(nameof(mms.OnSmtpDisconnected)); }; // Event raising if sending a single mail message fails mms.OnSendFailure += (smtpClient, sendFailureArgs) => { actualEvents.Push(nameof(mms.OnSendFailure)); }; // Event raising before sending a single mail message is finished mms.OnAfterSend += (smtpClient, afterSendArgs) => { actualEvents.Push(nameof(mms.OnAfterSend)); }; var recipient = new Recipient { Email = $"*****@*****.**", Name = $"Name of recipient" }; var mmm = new MailMergeMessage("Event tests" + somePlaceholder, "This is the plain text part for {Name} ({Email})") { Config = _settings.MessageConfig }; mmm.MailMergeAddresses.Add(new MailMergeAddress(MailAddressType.To, "{Name}", "{Email}")); var sequenceOfExpectedEvents = new List <string>(); if (withParseFailure) { sequenceOfExpectedEvents.Clear(); sequenceOfExpectedEvents.AddRange(new[] { nameof(mms.OnMessageFailure) /*, * nameof(mms.OnSmtpDisconnected)*/ }); } else { sequenceOfExpectedEvents.Clear(); sequenceOfExpectedEvents.AddRange(new[] { nameof(mms.OnBeforeSend), nameof(mms.OnSmtpConnected), nameof(mms.OnAfterSend), nameof(mms.OnSmtpDisconnected) }); } #endregion #region * Synchronous send method * try { mms.Send(mmm, recipient); } catch (Exception e) { Console.WriteLine(e.Message); } expectedEvents.Clear(); expectedEvents.PushRange(sequenceOfExpectedEvents.ToArray()); Assert.AreEqual(expectedEvents.Count, actualEvents.Count); // sequence of sync sending is predefined while (actualEvents.Count > 0) { expectedEvents.TryPop(out string expected); actualEvents.TryPop(out string actual); Assert.AreEqual(expected, actual); } #endregion #region * Async send method * actualEvents.Clear(); expectedEvents.Clear(); expectedEvents.PushRange(sequenceOfExpectedEvents.ToArray()); try { await mms.SendAsync(mmm, recipient); } catch (Exception e) { Console.WriteLine(e.Message); } Assert.AreEqual(expectedEvents.Count, actualEvents.Count); // sequence of async sending may be different from sync, but all events must exists var sortedActual = actualEvents.OrderBy(e => e).ToArray(); var sortedExpected = expectedEvents.OrderBy(e => e).ToArray(); for (var i = 0; i < sortedActual.Length; i++) { Assert.AreEqual(sortedExpected[i], sortedActual[i]); } #endregion mms.Dispose(); mmm.Dispose(); }