当前位置:编程学习 > C#/ASP.NET >>

请高手帮忙 程序异步执行 阻塞的 邮件验证问题


中间查询了DNS,还用SMTP协议去验证一个EMAIL到底可用不,这需要很多时间,并且有时候返回不了值,程序一直就在那儿阻塞了,我想到了一定时间,终止执行,所以用了异步执行,但中间,还是要在那儿阻塞起, 

高手帮一下,分大大的给,呵呵


public delegate string validateDele(string stremail, out string strReslut);
 public FrmMain()
        {
            InitializeComponent();
            threadvalidate = new Thread(new ThreadStart(DoValidate));
        }

 private void FrmMain_Load(object sender, EventArgs e)
        {
threadvalidate.Start();
         }
    private  void DoValidate()
        {
            while(blRunState)
            {
                
                DoingNow = "Task  Started";
            System.Threading.Thread.Sleep(100);
            try
            {
              
                DataUtility db = new DataUtility(strConn);

                String strsqlGetServer = "select * from EM_order by ItemID desc";
                DataTable tbServers = db.GetDataTable(strsqlGetServer);
                string strSql = "";
                DataTable dt = db.GetDataTable(strSql);
                MailChecker.MailChecker checker = new MailChecker.MailChecker();

                validateDele validateemail = new validateDele(DoSingleVlidate);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    
                    string strEmail = dt.Rows[i]["Email_varchar"].ToString().Trim();
                    string emailID = dt.Rows[i]["MemberID_int"].ToString();
                    if (dt.Rows[i]["ReMark"].ToString().Trim() == "CantValidateByCom")
                    {
                        string strUpdateSqlCanot = "Update set ReMark='CantValidateByCom',Enable_tinyint=0 where  Email_varchar=dbo.f_encryptEmail('" + strEmail + "')";
                        db.ExecSQL(strUpdateSqlCanot);
                        continue;
                    }

                    string strMailServer = strEmail.Substring(strEmail.IndexOf("@") + 1, strEmail.Length - strEmail.IndexOf("@") - 1);
                    bool isCanNotValidate = false;
                    for (int j = 0; j < tbServers.Rows.Count; j++)
                    {
                        if (strMailServer.ToLower() == tbServers.Rows[j]["MailServerName"].ToString().Trim().ToLower())
                        {
                            isCanNotValidate = true;
                            string strUpdateSqlCanot = "Update set ReMark='CantValidateByCom',Enable_tinyint=0 where  Email_varchar=dbo.f_encryptEmail('" + strEmail + "')";
                            db.ExecSQL(strUpdateSqlCanot);

                            break;
                        }
                    }
                    if (isCanNotValidate == true)
                    {
                            intHadValidateMaxID = int.Parse(emailID);
                            inthaveValidateCount++;
                            intBadEmailCount++;
                            continue;
                    }

                    DoingNow = "validating " + strEmail;
                    try
                    {
                        string reslsut = "";

                        IAsyncResult iAR = validateemail.BeginInvoke(strEmail, out reslsut, null, null); //异步
在这儿就阻塞了,可能几分钟都不不返回值,试着10S后如果还没有返回值的话就强制终止
                       Thread.Sleep(10000);

                        if (!iAR.IsCompleted)
                        {
                            validateemail.EndInvoke(out reslsut, iAR);
                        }
                       
                       // System.Threading.Thread.Sleep(300);
                        DoingNow = strEmail + " Validated Result: " + reslsut;
                        if (reslsut != null && reslsut != "" && reslsut.Substring(0, 2).ToLower() == "ok")
                        {
                            string strUpdateSql = "Update set Enable_tinyint=1 where  Email_varchar=dbo.f_encryptEmail('" + strEmail + "')";
                            db.ExecSQL(strUpdateSql);
                            intHadValidateMaxID = int.Parse(emailID);
                            inthaveValidateCount++;
                        }
                        else
                        {
                            string strUpdateSql = "Update set Enable_tinyint=0,ReMark='Validate Failed' where  Email_varchar=dbo.f_encryptEmail('" + strEmail + "')";
                            db.ExecSQL(strUpdateSql);
                            intHadValidateMaxID = int.Parse(emailID);
                            inthaveValidateCount++;
                            intBadEmailCount++;
                        }
                    }
                    catch (Exception ee)
                    {
                        //System.Windows.Forms.MessageBox.Show("发现错误,\n" + ee.Message + "\n" + ee.Source, "Runtime Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        continue;
                    }

                }
                DoingNow = "Task  Stoped";
                
            }
            catch (Exception ee)
            {
              //  System.Windows.Forms.MessageBox.Show("发现错误,\n" + ee.Message + "\n" + ee.Source, "Runtime Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                DoingNow = ee.Message;
             
            }
            finally
            {
                SetStatus();
               
               
                DoingNow = "Task  Stoped";
            }
 
            }  
          
         }
 public static string DoSingleVlidate(string strEmail,out string strReslut)
        {
            MailChecker.MailChecker checker = new MailChecker.MailChecker();
            try
            {
                strReslut = checker.CheckEmail(strEmail, strDNS);
            }
            catch (Exception ee)
            {
                strReslut = ee.Message;
            }
            return strReslut;
        }
--------------------编程问答-------------------- 主要是这几句代码
,      try
                    {
                        string reslsut = "";

                        IAsyncResult iAR = validateemail.BeginInvoke(strEmail, out reslsut, null, null); //异步
在这儿就阻塞了,可能几分钟都不不返回值,试着10S后如果还没有返回值的话就强制终止
                       Thread.Sleep(10000);

                        if (!iAR.IsCompleted)
                        {
                            validateemail.EndInvoke(out reslsut, iAR);
                        }
                       
                       // System.Threading.Thread.Sleep(300);
                        DoingNow = strEmail + " Validated Result: " + reslsut;
                        if (reslsut != null && reslsut != "" && reslsut.Substring(0, 2).ToLower() == "ok")
                        {
                            string strUpdateSql = "Update set Enable_tinyint=1 where  Email_varchar=dbo.f_encryptEmail('" + strEmail + "')";
                            db.ExecSQL(strUpdateSql);
                            intHadValidateMaxID = int.Parse(emailID);
                            inthaveValidateCount++;
                        }
--------------------编程问答--------------------  IAsyncResult iAR = validateemail.BeginInvoke(strEmail, out reslsut, null, null); //异步
在这儿就阻塞了,可能几分钟都不不返回值,试着10S后如果还没有返回值的话就强制终止
                       Thread.Sleep(10000);

                        if (!iAR.IsCompleted)
                        {
                            validateemail.EndInvoke(out reslsut, iAR);
                        }
                       
                       // System.Threading.Thread.Sleep(300);
                        DoingNow = strEmail + " Validated Result: " + reslsut;
好像没有效果,程序还是会阻塞 --------------------编程问答-------------------- 没人呢,哎,自己顶,

下面是我给别的朋友解释的
аoé<niuzhenhua@hotmail.com> 21:17:53
干嘛非要超时呢
  使用多线程搞定
45068163(45068163) 21:19:31
开了另外一个线程来处理的,
45068163(45068163) 21:20:06
没有用主线程处理,
аoé<niuzhenhua@hotmail.com> 21:20:28
验证dns 本来就支持 异步处理 
  好像是什么begin  -- 和 end什么.
Shield-Hand(30325464) 21:20:48
恩,异步线程
аoé<niuzhenhua@hotmail.com> 21:21:22
使用多线程怎么会 阻塞呢?
45068163(45068163) 21:21:36
我是调用的一外第三方的组件,写得不怎么好,
45068163(45068163) 21:22:18
输入一个EMAIL 然后去验证EMAIL到底是否真实,有时候要很久很久才返回值,有时候根本都返不回来
аoé<niuzhenhua@hotmail.com> 21:22:25
验证一个dns干嘛使用第三方空间
45068163(45068163) 21:23:19
不是验证DNS,中间有一步是验证MX记录,然后再用SMTP协议去验证邮箱是不是真实的
аoé<niuzhenhua@hotmail.com> 21:25:39
不是所有的邮箱 都能被验证的
45068163(45068163) 21:26:44
是呀,aol 的不支持,还有其它的,
аoé<niuzhenhua@hotmail.com> 21:27:21
s所以说 注册的人就郁闷了 
    注册不进去怎么办,

45068163(45068163) 21:28:20
不是的,我们有一个数据库,里面有30万多条客户填入的邮箱,现在要一条一条的读出来验证,把没用的删除
аoé<niuzhenhua@hotmail.com> 21:32:52
每个人发一个 邮件 节日祝贺信息  , 不成功的删除就ok
一般程序都有这功能
45068163(45068163) 21:33:45
呵呵,为什么要删除没用的,就是为了减少成为垃圾邮件的机率,你说的那种,什么叫成功和没有成功呢,执行JS?
аoé<niuzhenhua@hotmail.com> 21:34:01
no no 
аoé<niuzhenhua@hotmail.com> 21:34:57
事实上 用户有用没用不能那邮箱来判断
45068163(45068163) 21:35:04
大家有没有异步处理,或者多线程异步的代码,参考一下设计模式啦
аoé<niuzhenhua@hotmail.com> 21:35:18
迟早要吧数据库人删掉一半
45068163(45068163) 21:35:19
但大概可以,
аoé<niuzhenhua@hotmail.com> 21:35:24
 
аoé<niuzhenhua@hotmail.com> 21:35:34
劝你不要使用这个方式判断
аoé<niuzhenhua@hotmail.com> 21:35:57
数据库有记录用户上次登录时间的吗?
45068163(45068163) 21:36:15
因为是模拟SMTP协议发信,只是在没有传输数据,哎,没法呀,MANAGER要求这么做呀,郁闷呢
аoé<niuzhenhua@hotmail.com> 21:36:16
有一年没登录的全干掉不就ok
45068163(45068163) 21:37:22
呵呵,是公司的营销邮件系统,里面30多万EMAIL,并且全是国外的业务邮箱,什么样的后缀都可以看到 --------------------编程问答-------------------- 异步调用后就不用管了,在异步回来的时候进行处理就行了呀 --------------------编程问答--------------------
异步的话,可以设置一个超时时间啊,再怎么连不上,超时了应该回来了吧?

--------------------编程问答-------------------- 如果回不来怎么办呢
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,