Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在特定时间后停止进程运行?我能用定时器吗?_C#_.net_Timer - Fatal编程技术网

C# 如何在特定时间后停止进程运行?我能用定时器吗?

C# 如何在特定时间后停止进程运行?我能用定时器吗?,c#,.net,timer,C#,.net,Timer,用户表单有两个字段:电子邮件地址和验证状态(在.Net中)。 一旦用户保存表单数据,我就会调用电子邮件验证插件。 要求是,如果电子邮件验证时间超过5秒,则应停止验证并将电子邮件验证状态更新为无法验证 我使用定时器编写了以下代码,一旦定时器触发已用事件,我的控件将转到OnTimedEvent。当计时器过期时,我想更新线程ValidateEmailAddress中的isvalid=newoptionsetvalue(3)。我怎样才能做到这一点 internal void Valida

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

我使用
定时器
编写了以下代码,一旦定时器触发
已用
事件,我的控件将转到
OnTimedEvent
。当计时器过期时,我想更新线程ValidateEmailAddress中的
isvalid=newoptionsetvalue(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");
   } 
}