Poll email using async waiting for C #

I am creating a console application that:

  • Call the email account verification method (I took this step)
  • Convert attachemnt to pdf (I took this step)
  • Then, once the conversion is completed, wait 30 seconds.
  • Repeat the previous 3 steps.

I followed steps 1) and 2) in the ProcessMailMessages() method. The following code works, but I want to know if I am on the right track or is there a better way to poll the email client?

  private static int secondsToWait = 30 * 1000; static void Main(string[] args) { bool run = true; do { try { Task theTask = ProcessEmailTaskAsync(); theTask.Wait(); } catch (Exception e) { Debug.WriteLine("<p>Error in Client</p> <p>Exception</p> <p>" + e.Message + "</p><p>" + e.StackTrace + "</p> "); } GC.Collect(); } while (run); } static async Task ProcessEmailTaskAsync() { var result = await EmailTaskAsync(); } static async Task<int> EmailTaskAsync() { await ProcessMailMessages(); await Task.Delay(secondsToWait); return 1; } static async Task ProcessMailMessages() { ............................................................................... } 
+4
source share
3 answers

Instead of looping around basically, you can use a timer. Basically you have to set up a timer, and then you can just wait on the Console.Readline () console so that the console does not close.

Edit - here is an example

 using System; namespace ConsoleApplication1 { class Program { private const int MilliSecondsToWait = 30000; private static System.Timers.Timer EmailTimer; static void Main(string[] args) { EmailTimer = new System.Timers.Timer(MilliSecondsToWait); EmailTimer.Elapsed += EmailTimer_Elapsed; EmailTimer.Start(); Console.WriteLine("Press Enter to quit."); Console.ReadLine(); // if you hit enter, the app will exit. It is possible for the user to exit the app while a mail download is occurring. // I'll leave it to you to add some flags to control that situation (just trying to keep the example simple) } private static void EmailTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { // stop the timer to prevent overlapping email downloads if the current download takes longer than MilliSecondsToWait EmailTimer.Stop(); try { Console.WriteLine("Email Download in progress."); // get your email. } catch (System.Exception ex) { // handle any errors -- if you let an exception rise beyond this point, the app will be terminated. } finally { // start the next poll EmailTimer.Start(); } } } }
using System; namespace ConsoleApplication1 { class Program { private const int MilliSecondsToWait = 30000; private static System.Timers.Timer EmailTimer; static void Main(string[] args) { EmailTimer = new System.Timers.Timer(MilliSecondsToWait); EmailTimer.Elapsed += EmailTimer_Elapsed; EmailTimer.Start(); Console.WriteLine("Press Enter to quit."); Console.ReadLine(); // if you hit enter, the app will exit. It is possible for the user to exit the app while a mail download is occurring. // I'll leave it to you to add some flags to control that situation (just trying to keep the example simple) } private static void EmailTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { // stop the timer to prevent overlapping email downloads if the current download takes longer than MilliSecondsToWait EmailTimer.Stop(); try { Console.WriteLine("Email Download in progress."); // get your email. } catch (System.Exception ex) { // handle any errors -- if you let an exception rise beyond this point, the app will be terminated. } finally { // start the next poll EmailTimer.Start(); } } } } 
+2
source

your code works well, avoiding the use of timers! and also make your code asynchronous with asynchronous / pending (TPL). You are on the right track!

0
source

This is free use of async / await, as it is a console application, and you simply block it until the call ends. You can also simply call ProcessMailMessages () from within the do / while loop and do with it.

0
source

Source: https://habr.com/ru/post/1446343/


All Articles