如何在特定时间后停止进程运行?我可以用定时器吗

本文关键字:运行 我可以 定时器 进程 定时间 | 更新日期: 2023-09-27 18:27:05

用户表单有两个字段电子邮件地址和验证状态(在.Net中)。一旦用户保存表单数据,我就调用电子邮件验证插件。要求是,如果电子邮件验证耗时超过5秒,则应停止验证并将电子邮件验证状态更新为无法验证。

我使用Timer编写了以下代码,一旦计时器触发Elapsed事件,我的控制将转到OnTimedEvent。当计时器过去时,我想更新线程ValidateEmailAddress中的isvalid = new OptionSetValue(3)。我怎样才能做到这一点?

        internal void ValidateEmailAddress(ServiceClient sc, Entity target, string emailaddress, string IsValid, string remark, string message)
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
        System.Timers.Timer aTimer = new System.Timers.Timer();
        aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
        aTimer.Interval = 5000; // 5 second
        aTimer.Enabled = true;
        if (emailaddress != string.Empty || (ContainData(target, IsValid) && target[IsValid] == null))
        {
            string[] returnstr = (string[])sc.VerifyEmail(emailaddress);
            string resultValue = string.Empty;
            OptionSetValue isvalid = null;
            foreach (var item in returnstr)
            {
                if (!item.ToLower().ToString().Equals("success"))
                {
                    if (item.ToLower().ToString().Equals("serveriscatchall"))
                    {
                        isvalid = new OptionSetValue(1);
                    }
                    else
                        isvalid = new OptionSetValue(0);
                    resultValue = item;
                    break;
                }
                else
                {
                    isvalid = new OptionSetValue(1);
                }
            }
            if (isvalid != null)
            {
                target[IsValid] = isvalid;
            }
            if (resultValue.Length > 0)
            {
                string returnValue = GetSysConfig(resultValue);
                if (returnValue != null)
                {
                    target[remark] = returnValue;
                }
                else
                    target[remark] = GetSysConfig("Others") + "(Error : " + resultValue + ")";
            }
            else
            {
                target[remark] = null;
            }
        }
     // I need to capture timer elapse event here and then i will update email address as Not able to verify
     // How can i do this?   
}
static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
    {
        Console.WriteLine("0.5 seconds already over");
    } 

如何在特定时间后停止进程运行?我可以用定时器吗

如果我是你,我会在我的类中使用静态bool字段,类似于以下内容:

public class MyClass
{
   public static bool continueOperation = true;

internal void ValidateEmailAddress(ServiceClient sc, Entity target, string emailaddress, string IsValid, string remark, string message)
{
    continueOperation = true;
    AppDomain currentDomain = AppDomain.CurrentDomain;
    currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
    System.Timers.Timer aTimer = new System.Timers.Timer();
    aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
    aTimer.Interval = 5000; // 5 second
    aTimer.Enabled = true;
    if (emailaddress != string.Empty || (ContainData(target, IsValid) && target[IsValid] == null))
    {
        string[] returnstr = (string[])sc.VerifyEmail(emailaddress);
        string resultValue = string.Empty;
        OptionSetValue isvalid = null;
        foreach (var item in returnstr)
        {
            if(!continueOperation)
               break;
            {
              if (!item.ToLower().ToString().Equals("success"))
              {
                if (item.ToLower().ToString().Equals("serveriscatchall"))
                {
                    isvalid = new OptionSetValue(1);
                }
                else
                    isvalid = new OptionSetValue(0);
                resultValue = item;
                break;
              }
              else
              {
                isvalid = new OptionSetValue(1);
              }
        }
        if(!continueOperation)
            isvalid = new OptionSetValue(3);
        if (isvalid != null)
        {
            target[IsValid] = isvalid;
        }
        if (resultValue.Length > 0)
        {
            string returnValue = GetSysConfig(resultValue);
            if (returnValue != null)
            {
                target[remark] = returnValue;
            }
            else
                target[remark] = GetSysConfig("Others") + "(Error : " + resultValue + ")";
        }
        else
        {
            target[remark] = null;
        }
    }
 // I need to capture timer elapse event here and then i will update email address as Not able to verify
 // How can i do this?   
}
   static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
   {
       continueOperation = false;
       Console.WriteLine("0.5 seconds already over");
   } 
}