如何在星期一每周重置一个变量? [英] How to reset a variable in every week on monday?

查看:78
本文介绍了如何在星期一每周重置一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我有一个要求,比如每周发送25封邮件。所以我做了什么,我拿了一个计数,并在发送每封邮件时计算++。当它达到25时显示警报已经超过了本周的限制。但我的问题是如何在一周开始时重置该计数,即周一。

这里有一些我写过的代码。

 尝试 
{
string body = ;
string tbl_custInfo = ;
int countno1 = 1 ;
int countno = 1 ;
string xmlfile = Server.MapPath( 的app_code / count.xml)修剪()ToLower将()。;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(xmlfile);

XmlNodeList CountNodeList = xmldoc.SelectNodes( // section);

foreach (XmlNode CountNode in CountNodeList)
{
if (CountNode.HasChildNodes)
{
countno = Convert.ToInt32(CountNode.ChildNodes [ 0 ] InnerText.Trim()ToLower将());

var testDay = DateTime.Now.DayOfWeek.ToString();
// if(testDay ==Sunday)
// if(testDay ==Monday&& countno!= 1)
// {

// CountNode.ChildNodes [0] .InnerText = countno1.ToString();


// }

if (( testDay == Monday || testDay == 星期二 || testDay == 星期三 || testDay == 星期四 || testDay == Friday || testDay == 星期六 || testDay == 星期日)&& (countno < = 3 ))
{


tbl_custInfo = < table style ='border:solid 1px #eee; padding-removed30px;' align ='center'width ='100%'>;
tbl_custInfo + = < tr>< td colspan ='2'style ='border-removedsolid 1px#aec6d8; text-align:center; font-weight:bold; color:#8d8f8a;'>客户联系方式< / td>< / tr>;
tbl_custInfo + = < tr>< td style ='border-removedsolid 1px #eee;' >名称:< / td>< td style ='border-removedsolid 1px #eee;'> + txtName.Value.Trim()+ < / td>< / tr>;
tbl_custInfo + = < tr>< td style ='border-removedsolid 1px #eee;' >电话:< / td>< td style ='border-removedsolid 1px #eee;'> + txtPhone.Value.Trim()+ < / td>< / tr>;

tbl_custInfo + = < tr>< td style ='border-removedsolid 1px #eee;'>电子邮件:< / td>< td style ='border-removedsolid 1px #eee;'> + txtEmail.Value.Trim()+ < / td>< / tr>;

tbl_custInfo + = < tr>< td style ='border-removedsolid 1px #eee;'>消息:< / td>< td style ='border-removedsolid 1px #eee;'> + txtComment.Value.Trim()+ < / td>< / tr>;
tbl_custInfo + = < / table>< br />< br /> ;

body = < br /> + tbl_custInfo;

SmtpClient sc = new SmtpClient();
MailMessage mm = new MailMessage();

if (ConfigurationManager.AppSettings [ MailFrom]。ToString()。Trim()!=
{
mm.From = new MailAddress(ConfigurationManager.AppSettings [ MailFrom]。ToString()。Trim(),txtEmail.Value.ToString()。Trim());
}
if (ConfigurationManager.AppSettings [ MailTo]。ToString()。Trim()!=
{
mm.To.Add(ConfigurationManager.AppSettings [ MailTo]的ToString()修剪())。。
}
if (ConfigurationManager.AppSettings [ MailBCc]。ToString()。Trim()!=
{
mm.Bcc.Add(ConfigurationManager.AppSettings [ MailBCc]的ToString()修剪())。。
}
mm.Subject = ConfigurationManager.AppSettings [ 主题]。 。的ToString()修剪();
mm.ReplyTo = new MailAddress(txtEmail.Value);
mm.Body = body;

mm.IsBodyHtml = true ;

sc.Send(mm);


countno1 = countno1 + 1 ;
CountNode.ChildNodes [ 0 ]。InnerText = countno1.ToString();

xmldoc.Save(xmlfile);
this .ClientScript.RegisterStartupScript( this .GetType(), animo < script language = \javaScript \> + alert('谢谢联系我们。'); + window.location.href ='contact.aspx'; + < + / script>);

}
else
{
this .ClientScript.RegisterStartupScript( this .GetType(), animo < script language = \javaScript \> + alert('没有更多可用的时段,并在14(或7)天内重试。) ); + window.location.href ='contact.aspx'; + < + < span class =code-string> / script>
);
}

}

}
}
catch {}



请帮帮我。

解决方案

1.您应该将其实现到Windows服务应用程序而不是一个Web(ASP.NET)应用程序,因为只有在至少一个用户访问它时才激活Web应用程序。



2.所以解决方案是,实现一个Windows服务应用程序和它内部你可以使用像上面的第一个解决方案中建议的,但更好的是使用线程和主线程内部使用 Thread.Sleep (让线程休眠24小时),然后检查星期一,如下例所示:

 TimeSpan pauseTimeSpan =  new  TimeSpan( 24  0  0 );  //   24小时! 
TimeSpan weekTimeSpan = TimeSpan(24 * 7, 0 0 ); // 7天!
//
while true
{
if (DateTime.Now.DayOfWeek == DayOfWeek.Monday)&&
(workerThread == null ||!workerThread.IsAlive))
{
//
// 启动工作线程以发送电子邮件通知!
//
workerThread = new 线程( new ThreadStart(SendEmailNotifications));
workerThread.Start();
//
// 让主线程处于休眠状态7天!
//
Thread.Sleep(weekTimeSpan);
}
其他
{
// < span class =code-comment>

// 暂停主线程只有一天因为可能是这样的情况
// 服务器将在一周内重新启动!
//
Thread.Sleep(pauseTimeSpan);
}

}


您可以使用 System.Timer [ ^ ]对象。


请查看此链接中的代码 [ ^ ]

Hi Friends,
I have one requirement like to send 25 mail every week.So what i did,I took one count and made count++ on sending every mail.When it reached to 25 it displays alert already crossed limit for this week.But my problem is that how to reset that count on start of the week i.e on Monday.
Here some code that i have written.

try
        {
            string body = "";
            string tbl_custInfo = "";
            int countno1=1;
            int countno=1;
            string xmlfile = Server.MapPath("App_code/count.xml").Trim().ToLower();
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(xmlfile);

            XmlNodeList CountNodeList = xmldoc.SelectNodes("//section");

            foreach (XmlNode CountNode in CountNodeList)
            {
                if (CountNode.HasChildNodes)
                {
                    countno =Convert.ToInt32(CountNode.ChildNodes[0].InnerText.Trim().ToLower());
             
             var testDay = DateTime.Now.DayOfWeek.ToString();
             //if (testDay == "Sunday")
             //if (testDay == "Monday" && countno != 1)
             //{

             //    CountNode.ChildNodes[0].InnerText = countno1.ToString();
              

             //}
            
             if ((testDay == "Monday" || testDay == "Tuesday" || testDay == "Wednesday" || testDay == "Thursday" || testDay == "Friday" || testDay == "Saturday" || testDay == "Sunday") && (countno <= 3))
            {
               
            
            tbl_custInfo = "<table style='border:solid 1px #eee;padding-removed30px;' align='center' width='100%'>";
            tbl_custInfo += "<tr><td colspan='2' style='border-removedsolid 1px #aec6d8;text-align:center;font-weight:bold;color:#8d8f8a;'>Customer Contact Details</td></tr>";
            tbl_custInfo += "<tr><td style='border-removedsolid 1px #eee;'>Name :</td><td style='border-removedsolid 1px #eee;'>" + txtName.Value.Trim() + "</td></tr>";
            tbl_custInfo += "<tr><td style='border-removedsolid 1px #eee;'>Phone :</td><td style='border-removedsolid 1px #eee;'>" + txtPhone.Value.Trim() + "</td></tr>";
           
            tbl_custInfo += "<tr><td style='border-removedsolid 1px #eee;'>Email :</td><td style='border-removedsolid 1px #eee;'>" + txtEmail.Value.Trim() + "</td></tr>";

            tbl_custInfo += "<tr><td style='border-removedsolid 1px #eee;'>Message :</td><td style='border-removedsolid 1px #eee;'>" + txtComment.Value.Trim() + "</td></tr>";
            tbl_custInfo += "</table><br/><br/>";

            body = "<br/>" + tbl_custInfo;
           
            SmtpClient sc = new SmtpClient();
            MailMessage mm = new MailMessage();

            if (ConfigurationManager.AppSettings["MailFrom"].ToString().Trim() != "")
            {
                mm.From = new MailAddress(ConfigurationManager.AppSettings["MailFrom"].ToString().Trim(), txtEmail.Value.ToString().Trim());
            }
            if (ConfigurationManager.AppSettings["MailTo"].ToString().Trim() != "")
            {
                mm.To.Add(ConfigurationManager.AppSettings["MailTo"].ToString().Trim());
            }
            if (ConfigurationManager.AppSettings["MailBCc"].ToString().Trim() != "")
            {
                mm.Bcc.Add(ConfigurationManager.AppSettings["MailBCc"].ToString().Trim());
            }
            mm.Subject = ConfigurationManager.AppSettings["Subject"].ToString().Trim();
            mm.ReplyTo = new MailAddress(txtEmail.Value);
            mm.Body = body;

            mm.IsBodyHtml = true;

            sc.Send(mm);
            
            
            countno1 = countno1 + 1;
            CountNode.ChildNodes[0].InnerText = countno1.ToString();
          
            xmldoc.Save(xmlfile);
            this.ClientScript.RegisterStartupScript(this.GetType(), "animo", "<script language=\"javaScript\">" + "alert('Thanks for Contacting Us.');" + "window.location.href='contact.aspx';" + "<" + "/script>");

     }
    else
    {
                this.ClientScript.RegisterStartupScript(this.GetType(), "animo", "<script language=\"javaScript\">" + "alert('no more available time slots and to try again in 14 (or 7) days.');" + "window.location.href='contact.aspx';" + "<" + "/script>");
            }
          
       }
          
                  }
        }
        catch { }


Please help me out.

解决方案

1.You should implement this into a windows service application and not into a web (ASP.NET) application, because the web application is activated only if at least one user is accessing it.

2.So the solution is, to implement a windows service application and inside of it you could use Timer like was suggested in the 1st solution above, but better is to use Thread and inside of the main thread to use Thread.Sleep (to put the thread to sleep for 24 hours) and then to check for Monday like in the example bellow:

TimeSpan pauseTimeSpan = new TimeSpan(24, 0, 0); //24 hours!
TimeSpan weekTimeSpan = new TimeSpan(24*7, 0, 0); //7 days!
//
while (true)
                {
                    if(DateTime.Now.DayOfWeek == DayOfWeek.Monday) &&
                        (workerThread == null || !workerThread.IsAlive))
                    {
                        //
                        // Start the Worker thread for sending email notifications!
                        //
                        workerThread = new Thread(new ThreadStart(SendEmailNotifications));
                        workerThread.Start();
                        //
                        // Put the main thread in sleep for 7 days!
                        //
                        Thread.Sleep(weekTimeSpan);
                    }
                    else
                    {
                       //
                       // Pause the main thread only one day because could be the situation that
                       // the server will be restarted during the week!
                       //
                       Thread.Sleep(pauseTimeSpan);
                    }   
                    
                }


You can use a System.Timer[^] object.


Please go though the codes here in this Link[^]


这篇关于如何在星期一每周重置一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆