用户表单有2个字段的电子邮件地址和验证状态(in.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");
}
答案 0 :(得分:0)
如果我是你,我会在班上使用静态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");
}
}