短信发送程序部署在服务器上发了几天又不发了,跪求解决方案! 各位大侠,小弟用winform开发了一个短信发送程序,用于定时发送短信。部署到服务器上以后开始一周左右时间每天都定时发送,正常,但是之后几天就没有定时发送,程序依然在服务器上运行,重启服务器后又好了,不知是什么原因,求高人指导! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用线程还是timer?线程估计是死锁,很不好处理的 用的是timer,本机模拟(通过改变本地时间)连续10天发送都没问题,到服务器上就出那个问题了 我也写过,分短信上行和短信下行的,调用电信或者移动提供的webservice短信接口的,你代码处理数据时可能对脏数据没有处理好 不是短信猫,是调用电信的短信发送接口,dll版的,目前还没查出具体原因,只能在服务器上设置一个计划任务。努力查原因中 还有就是看看业务逻辑的代码有没有潜在的bug private bool flag = true; private string today = string.Empty; private string yeaterday = string.Empty; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.timer1.Enabled = true; this.MaximizeBox = false; CloseButtonEnable(); //SenMessage(); } protected bool SenMessage() { bool flag = true; //bool returnValue; //正式账号 string uid = "3SDK-EMS-0130-LHWMP"; //测试账号 //string uid = "0SDK-ESH-0130-LIQQK"; DataSet ds1 = new DataSet(); String _Response = ""; string m_content = string.Empty; string mobile = string.Empty; string user_id = string.Empty; string m_id = string.Empty; //using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString())) //{ //发送状态 //数据库连接字符串 //服务器测试数据库 //string connectstring = @"server =222.73.42.174,7683;uid=boliwei;pwd=boliwei)*)#;database=Propecia_CDB_Test"; //本机 string connectstring = @"server =SHA98-INT004\SQL2005;uid=sa;pwd=sa;database=Propecia_CDB"; //正式数据库 //string connectstring = @"server =222.73.42.174,7683;uid=boliwei;pwd=boliwei)*)#;database=Propecia_CDB"; string sql = "SELECT fMID,fPhone,fStartTreatDate,DATEDIFF(DAY,fStartTreatDate,GETDATE())AS day_differ FROM dbo.Tb_Member WHERE fIsJoinAGA=1 AND fIsTreat=1 AND isnull(fStartTreatDate,'')> DATEADD(YEAR,-1,GETDATE()) AND isnull(fStartTreatDate,'')<DATEdiff(DAY,7,GETDATE()) AND ISNULL(fPhone,'')<>'' AND fStatus<>2 ORDER BY fStartTreatDate"; //string sql = "sp_GetFphone"; string sql2 = "SELECT * FROM [Tb_message]"; SqlConnection con = new SqlConnection(connectstring); //SqlCommand cmd1 = new SqlCommand(sql, con); //cmd1.CommandType = CommandType.StoredProcedure; con.Open(); SqlDataAdapter adp1 = new SqlDataAdapter(sql, con); //SqlDataAdapter adp1 = new SqlDataAdapter(cmd1); adp1.Fill(ds1); SqlDataAdapter adp2 = new SqlDataAdapter(sql2, con); DataSet ds2 = new DataSet(); adp2.Fill(ds2); string[] aray = new string[31]; //短信内容m_content for (int i = 0; i < ds2.Tables[0].Rows.Count; i++) { aray[i] = ds2.Tables[0].Rows[i]["m_content"].ToString(); } #region for (int i = 0; i < ds1.Tables[0].Rows.Count; i++) { int day_differ; day_differ = Convert.ToInt32(ds1.Tables[0].Rows[i]["day_differ"].ToString()); mobile = ds1.Tables[0].Rows[i]["fPhone"].ToString(); user_id = ds1.Tables[0].Rows[i]["fMID"].ToString(); switch (day_differ) { case 7: m_content = aray[0]; break; case 14: m_content = aray[1]; break; case 21: m_content = aray[2]; break; case 28: m_content = aray[3]; break; case 35: m_content = aray[4]; break; case 42: m_content = aray[5]; break; case 49: m_content = aray[6]; break; case 56: m_content = aray[7]; break; case 63: m_content = aray[8]; break; case 70: m_content = aray[9]; break; case 77: m_content = aray[10]; break; case 84: m_content = aray[11]; break; case 91: m_content = aray[12]; break; case 98: m_content = aray[13]; break; case 105: m_content = aray[14]; break; case 112: m_content = aray[15]; break; case 119: m_content = aray[16]; break; case 126: m_content = aray[17]; break; case 133: m_content = aray[18]; break; case 140: m_content = aray[19]; break; case 147: m_content = aray[20]; break; case 154: m_content = aray[21]; break; case 161: m_content = aray[22]; break; case 168: m_content = aray[23]; break; case 201: m_content = aray[24]; break; case 234: m_content = aray[25]; break; case 267: m_content = aray[26]; break; case 300: m_content = aray[27]; break; case 333: m_content = aray[28]; break; case 365: m_content = aray[29]; break; default: m_content = ""; break; } //判断该用户当天是否已经发送过,用于服务器死机或者重启造成的情况 string sql4 = "select 1 from Tb_sendState where mobile='" + mobile + "' and senddate like '%"+DateTime.Now.ToShortDateString()+"%'"; SqlCommand cmd4 = new SqlCommand(sql4, con); string str_result = string.Empty; if (cmd4.ExecuteScalar() == null) { if (!m_content.Equals("")) { m_content += "[AGA中心]"; //即时发送 这里是软件序列号 手机号 短信内容 优先级 //int result = EUCPComm.SendSMS(uid, mobile, m_content, "5"); int result = 1; if (result == 1) _Response = "发送成功"; else if (result == 101) _Response = "网络故障"; else if (result == 102) _Response = "其它故障"; else if (result == 0) _Response = "失败"; else if (result == 100) _Response = "序列号码为空或无效"; else if (result == 107) _Response = "手机号码为空或者超过1000个"; else if (result == 108) _Response = "手机号码分割符号不正确"; else if (result == 109) _Response = "部分手机号码不正确,已删除,其余手机号码被发送"; else if (result == 110) _Response = "短信内容为空或超长(70个汉字)"; else if (result == 201) _Response = "计费失败,请充值"; else _Response = "其他故障值:" + result.ToString(); //将发送情况写入数据库 string sql3 = "insert into Tb_sendState(senddate,mobile,[USER_ID],message,sendstate) values ('" + DateTime.Now.ToString() + "','" + mobile + "','" + user_id + "','" + m_content + "','" + _Response + "')"; SqlCommand cmd2 = new SqlCommand(sql3, con); cmd2.ExecuteNonQuery(); // } flag = false; //这里随便设置一个过去的日期 yeaterday = "2011-3-29"; //yeaterday = DateTime.Now.ToShortDateString(); } } } #endregion con.Close(); return flag; } private void timer1_Tick(object sender, EventArgs e) { today = DateTime.Now.ToShortDateString(); //if (!today.Equals(yeaterday)) //{ // flag = true; //} if (!today.Equals(yeaterday)) { this.timer2.Enabled = true; } else { this.timer2.Enabled = false; } } private void timer2_Tick(object sender, EventArgs e) { //string date = DateTime.Now.ToShortDateString(); string time = DateTime.Now.ToShortTimeString(); if (flag && (time.Equals("13:52"))) { SenMessage(); } } 以上是短信发送代码,通过两个timer控件控制 timer1和timer2都干嘛的啊,能否说说啊 private void timer2_Tick(object sender, EventArgs e) { //string date = DateTime.Now.ToShortDateString(); string time = DateTime.Now.ToShortTimeString(); if (flag && (time.Equals("13:52"))) { SenMessage(); } }看这段代码,有问题,如果senMessage()执行得很长时间,你timer2设置的时间间隔不够长的,那就乱套了 改良的方法之一就是加语句如下timer2.enable=false;SenMessage();timer2.enable=true; 问题不是他发不完,是压根就不给我发出去了,还有这个项目调的是亿美短信的一个dll没用短信猫。估计不是21楼的原因吧,不过还是十分感谢21楼的同志。 按照楼主刚才说的,重启一下又好了,说明是问题很大部分在你这边的代码里。 不过楼主直接这样写private void timer2_Tick(object sender, EventArgs e) { //string date = DateTime.Now.ToShortDateString(); string time = DateTime.Now.ToShortTimeString(); if (flag && (time.Equals("13:52"))) { SenMessage(); } } 100% 是有问题的 time.Equals("13:52")频率没设好,这句肯定会有问题的 C#实现自定义通讯协议 读取数据库字段进数组 批量更新问题 C#界面设计 关于二进制位操作的问题 c#数组问题,谁来解决下 关于HttpWebRequest的一个问题,请大家来帮忙(在线等) Class中的值类型子项是值类型还是引用类型 怎样在From1中调用Class1里的函数? 返回值问题 关于资源文件问题 如何统计一段时间里的上班时间(周一至周五)和周末时间(周六、日)
private bool flag = true;
private string today = string.Empty;
private string yeaterday = string.Empty; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Enabled = true;
this.MaximizeBox = false;
CloseButtonEnable();
//SenMessage();
} protected bool SenMessage()
{
bool flag = true;
//bool returnValue;
//正式账号
string uid = "3SDK-EMS-0130-LHWMP";
//测试账号
//string uid = "0SDK-ESH-0130-LIQQK";
DataSet ds1 = new DataSet();
String _Response = "";
string m_content = string.Empty;
string mobile = string.Empty;
string user_id = string.Empty;
string m_id = string.Empty; //using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"].ToString()))
//{ //发送状态
//数据库连接字符串
//服务器测试数据库
//string connectstring = @"server =222.73.42.174,7683;uid=boliwei;pwd=boliwei)*)#;database=Propecia_CDB_Test";
//本机
string connectstring = @"server =SHA98-INT004\SQL2005;uid=sa;pwd=sa;database=Propecia_CDB";
//正式数据库
//string connectstring = @"server =222.73.42.174,7683;uid=boliwei;pwd=boliwei)*)#;database=Propecia_CDB";
string sql = "SELECT fMID,fPhone,fStartTreatDate,DATEDIFF(DAY,fStartTreatDate,GETDATE())AS day_differ FROM dbo.Tb_Member WHERE fIsJoinAGA=1 AND fIsTreat=1 AND isnull(fStartTreatDate,'')> DATEADD(YEAR,-1,GETDATE()) AND isnull(fStartTreatDate,'')<DATEdiff(DAY,7,GETDATE()) AND ISNULL(fPhone,'')<>'' AND fStatus<>2 ORDER BY fStartTreatDate";
//string sql = "sp_GetFphone";
string sql2 = "SELECT * FROM [Tb_message]"; SqlConnection con = new SqlConnection(connectstring); //SqlCommand cmd1 = new SqlCommand(sql, con);
//cmd1.CommandType = CommandType.StoredProcedure; con.Open();
SqlDataAdapter adp1 = new SqlDataAdapter(sql, con);
//SqlDataAdapter adp1 = new SqlDataAdapter(cmd1);
adp1.Fill(ds1); SqlDataAdapter adp2 = new SqlDataAdapter(sql2, con);
DataSet ds2 = new DataSet();
adp2.Fill(ds2); string[] aray = new string[31]; //短信内容m_content
for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
{ aray[i] = ds2.Tables[0].Rows[i]["m_content"].ToString();
} #region for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
int day_differ;
day_differ = Convert.ToInt32(ds1.Tables[0].Rows[i]["day_differ"].ToString());
mobile = ds1.Tables[0].Rows[i]["fPhone"].ToString();
user_id = ds1.Tables[0].Rows[i]["fMID"].ToString(); switch (day_differ)
{
case 7:
m_content = aray[0];
break;
case 14:
m_content = aray[1];
break; case 21:
m_content = aray[2];
break;
case 28:
m_content = aray[3];
break;
case 35:
m_content = aray[4];
break;
case 42:
m_content = aray[5];
break;
case 49:
m_content = aray[6];
break;
case 56:
m_content = aray[7];
break;
case 63:
m_content = aray[8];
break;
case 70:
m_content = aray[9];
break;
case 77:
m_content = aray[10];
break;
case 84:
m_content = aray[11];
break;
case 91:
m_content = aray[12];
break;
case 98:
m_content = aray[13];
break;
case 105:
m_content = aray[14];
break;
case 112:
m_content = aray[15];
break;
case 119:
m_content = aray[16];
break;
case 126:
m_content = aray[17];
break;
case 133:
m_content = aray[18];
break;
case 140:
m_content = aray[19];
break;
case 147:
m_content = aray[20];
break;
case 154:
m_content = aray[21];
break;
case 161:
m_content = aray[22];
break;
case 168:
m_content = aray[23];
break;
case 201:
m_content = aray[24];
break;
case 234:
m_content = aray[25];
break;
case 267:
m_content = aray[26];
break;
case 300:
m_content = aray[27];
break;
case 333:
m_content = aray[28];
break;
case 365:
m_content = aray[29];
break;
default:
m_content = "";
break;
}
//判断该用户当天是否已经发送过,用于服务器死机或者重启造成的情况
string sql4 = "select 1 from Tb_sendState where mobile='" + mobile + "' and senddate like '%"+DateTime.Now.ToShortDateString()+"%'";
SqlCommand cmd4 = new SqlCommand(sql4, con);
string str_result = string.Empty;
if (cmd4.ExecuteScalar() == null)
{
if (!m_content.Equals(""))
{
m_content += "[AGA中心]";
//即时发送 这里是软件序列号 手机号 短信内容 优先级
//int result = EUCPComm.SendSMS(uid, mobile, m_content, "5");
int result = 1;
if (result == 1)
_Response = "发送成功";
else if (result == 101)
_Response = "网络故障";
else if (result == 102)
_Response = "其它故障";
else if (result == 0)
_Response = "失败";
else if (result == 100)
_Response = "序列号码为空或无效";
else if (result == 107)
_Response = "手机号码为空或者超过1000个";
else if (result == 108)
_Response = "手机号码分割符号不正确";
else if (result == 109)
_Response = "部分手机号码不正确,已删除,其余手机号码被发送";
else if (result == 110)
_Response = "短信内容为空或超长(70个汉字)";
else if (result == 201)
_Response = "计费失败,请充值";
else
_Response = "其他故障值:" + result.ToString(); //将发送情况写入数据库
string sql3 = "insert into Tb_sendState(senddate,mobile,[USER_ID],message,sendstate) values ('" + DateTime.Now.ToString() + "','" + mobile + "','" + user_id + "','" + m_content + "','" + _Response + "')";
SqlCommand cmd2 = new SqlCommand(sql3, con);
cmd2.ExecuteNonQuery();
// }
flag = false;
//这里随便设置一个过去的日期
yeaterday = "2011-3-29";
//yeaterday = DateTime.Now.ToShortDateString();
}
}
}
#endregion
con.Close();
return flag;
}
{
today = DateTime.Now.ToShortDateString();
//if (!today.Equals(yeaterday))
//{
// flag = true;
//}
if (!today.Equals(yeaterday))
{
this.timer2.Enabled = true;
}
else
{
this.timer2.Enabled = false;
}
} private void timer2_Tick(object sender, EventArgs e)
{
//string date = DateTime.Now.ToShortDateString(); string time = DateTime.Now.ToShortTimeString(); if (flag && (time.Equals("13:52")))
{
SenMessage();
} }
{
//string date = DateTime.Now.ToShortDateString(); string time = DateTime.Now.ToShortTimeString(); if (flag && (time.Equals("13:52")))
{
SenMessage();
} }看这段代码,有问题,如果senMessage()执行得很长时间,你timer2设置的时间间隔不够长的,那就乱套了
timer2.enable=false;
SenMessage();
timer2.enable=true;
{
//string date = DateTime.Now.ToShortDateString(); string time = DateTime.Now.ToShortTimeString(); if (flag && (time.Equals("13:52")))
{
SenMessage();
} } 100% 是有问题的