c# - 使用 async await c# 轮询电子邮件

标签 c#

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

  1. 调用一个方法来检查一个电子邮件帐户(我已经完成了这一步)
  2. 将附件转换为pdf(我已经完成了这一步)
  3. 然后在转换完成后等待 30 秒
  4. 不断重复前面的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()
    {
        ...............................................................................
    }

最佳答案

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

        }

    }
}

关于c# - 使用 async await c# 轮询电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13407095/

相关文章:

c# - 如何以编程方式将代码插入程序集

c# - 动态导航 2009 RE : SoapException: Standard Item Code 'foo' does not exist

c# - 在运行时从 XSD 生成类

c# - 从 C# 代码启动 Outlook 2013

c# - 在 C# 中创建一个额外的桌面

c# - 如何将用户控件添加到组框

c# - wpf 组合框绑定(bind)

c# - 将 WPF 控件绑定(bind)到 Timer 中更新的数据是否安全?

c# - 领域驱动设计-父子关系模式-规范模式

c# - 一个连接的多个 SQL 命令