C# 使用异步等待c轮询电子邮件#

C# 使用异步等待c轮询电子邮件#,c#,C#,我正在创建一个控制台应用程序,它将: 调用方法检查电子邮件帐户(我已完成此步骤) 将附件转换为pdf(我已完成此步骤) 转换完成后,等待30秒 连续重复前面的3个步骤 我已经在ProcessMailMessages()方法中完成了步骤1)和2)。 下面的代码可以工作,但我想知道我是否在正确的轨道上,或者是否有更好的方法来轮询电子邮件客户端 private static int secondsToWait = 30 * 1000; static void Main(string[

我正在创建一个控制台应用程序,它将:

  • 调用方法检查电子邮件帐户(我已完成此步骤)
  • 将附件转换为pdf(我已完成此步骤)
  • 转换完成后,等待30秒
  • 连续重复前面的3个步骤
  • 我已经在
    ProcessMailMessages()
    方法中完成了步骤1)和2)。 下面的代码可以工作,但我想知道我是否在正确的轨道上,或者是否有更好的方法来轮询电子邮件客户端

        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()
        {
            ...............................................................................
        }
    
    private static int secondsToWait=30*1000;
    静态void Main(字符串[]参数)
    {
    bool run=true;
    做
    {
    尝试
    {
    任务任务=ProcessEmailTaskAsync();
    任务。等待();
    }
    捕获(例外e)
    {
    Debug.WriteLine(“客户端错误

    异常

    ”+e.Message+”

    “+e.StackTrace+”

    ”); } GC.Collect(); }边跑边跑; } 静态异步任务进程EmailTaskAsync() { var result=await-EmailTaskAsync(); } 静态异步任务EmailTaskAsync() { 等待ProcessMailMessages(); 等待任务。延迟(第二次等待); 返回1; } 静态异步任务ProcessMailMessages() { ............................................................................... }
    您可以使用计时器,而不是在main中循环。大体上,您可以设置计时器,然后在Console.Readline()上等待,以防止控制台关闭

    编辑--下面是一个示例

    
    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();
                }
    
            }
    
        }
    }
    

    您的代码运行良好,避免了使用计时器!还可以使用async/await(TPL)使代码异步。你在正确的轨道上

    这是对async/await的无端使用,因为它是一个控制台应用程序,您只是在阻塞,直到调用完成为止。也可以从do/while循环中调用ProcessMailMessages(),然后处理它。

    如果使用IMAP,则可以让服务器在消息到达时立即通知您。