Asp.net 2008,在线程里和窗体上调用同一个方法,为什么效果不一样,有部分代码没走??

ttf1 2009-09-07 10:09:45
本人用asp.net2008做了个短信系统,在Global.asax线程里半分钟,进行一次短信的收发,问题是有部份正常代码不执行,不知为什么?或有什么好方法来解决半分钟执行一次,页面刷新是一个方法可能?还有其它的吗?

Asp.net 2008,在线程里和窗体上调用同一个方法,为什么效果不一样,有部分代码没走??
在下面(1)和(2)中调用(3)中的一个方法ReceivedMsg(),可是在(1)调用时不执行(3)中的getSMSContent()这个静态方法,而在(2)调用时就执行,
请问为什么线程执行方式和普通的不一样?
怎么样修改成能执行的呢?


(1).Global.asax线程代码:
private System.Threading.Thread thdAuto = null;

void thdOperator(object sender)
{
while (true)
{
ZHSMS.SMS.BLL.SmsBll Sms = new ZHSMS.SMS.BLL.SmsBll(ZHSMS.SMS.BLL.Common.checkUserName, ZHSMS.SMS.BLL.Common.checkPassword, ZHSMS.SMS.BLL.Common.checkDateTime);

//添加接收记录
Sms.ReceivedMsg();

System.Threading.Thread.Sleep(30000);
}
}

protected void Application_Start(object sender, EventArgs e)
{
System.Threading.ParameterizedThreadStart thdStart = new System.Threading.ParameterizedThreadStart(thdOperator);
thdAuto = new System.Threading.Thread(thdStart);
thdAuto.Start();
}


(2).SendMessage.aspx窗体按钮事件中代码:
protected void btnSend_Click(object sender, EventArgs e)
{
string strValue;

ZHSMS.SMS.BLL.SmsBll Sms = new ZHSMS.SMS.BLL.SmsBll(ZHSMS.SMS.BLL.Common.checkUserName, ZHSMS.SMS.BLL.Common.checkPassword, ZHSMS.SMS.BLL.Common.checkDateTime);

//添加接收记录
Sms.ReceivedMsg();
}


(3).SmsBll.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Runtime.InteropServices;
using ZHSMS.SMS.Data.ACC;

namespace ZHSMS.SMS.BLL
{
public class SmsBll
{
private int eModemStatus;
private const int OK_STATUS = 1;
private SmsAcc smsAcc = new SmsAcc();
private static string fromNumber;
private static string arrivedTime;
private static string msgContent;
private static int msgType;

[DllImport("EUCPComm.dll", EntryPoint = "ReceiveSMS")] // 接收短信
public static extern int ReceiveSMS(string sn, deleSQF mySmsContent);

//回调(接收短信)
static void getSMSContent(string mobile, string senderaddi, string recvaddi, string ct, string sd, ref int flag)
{
//string mob = mobile;
//string content = ct;
//int myflag = flag;
//MessageBox.Show(mob + "----" + content);
fromNumber = mobile;
arrivedTime = sd;
msgContent = ct;
msgType = flag;
}

public delegate void deleSQF(string mobile, string senderaddi, string recvaddi, string ct, string sd, ref int flag);
deleSQF mySmsContent = new deleSQF(getSMSContent);

public string ReceivedMsg()
{
try
{
//网关发送短信
if (eModemStatus != OK_STATUS)
{
if (!SmsConn())
{
return "未注册成功";
}
}

deleSQF mySmsContent = new deleSQF(getSMSContent);
//接收短信 序列号 函数指针
int result = 2;
while (result == 2) //当result = 2 时,说明还有下一批短信等待接收,这时需重新再调用一次ReceiveSMS方法
{
fromNumber = "";
arrivedTime = "";
msgContent = "";
msgType = 0;
result = ReceiveSMS(Common.SysInfo.strSerialNumber, mySmsContent);
if (result == 1 && msgType == 1)
{
//将短信接收内容添加到ACCESS库中
smsAcc.InsertReceivedMsg(fromNumber, msgContent, Common.StringToDate(arrivedTime), "9");
}
else if (result == 101)
return "网络故障";
else if (result == 102)
return "其它故障";
else if (result == 105)
return "参数指针为空";
else if (result == 0)
return "失败";
else if (result == -1)
return "未知故障";
}
}
catch (Exception Ex)
{
return Ex.ToString();
}
finally
{
//关闭短信联接
SmsDisConn();
}
return "";
}


}
}
...全文
82 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttf1 2009-09-07
  • 打赏
  • 举报
回复
deleSQF 委托没有问题,只是在线程里调用时不走委托里的getSMSContent方法,而在窗体按钮单击时就能调用到委托进去,
winner2050 2009-09-07
  • 打赏
  • 举报
回复
不过就算你解决了bug,这个定时功能也没有实际用途。

网站20分钟没有人访问,iis 就回收掉了。一切都停止了。

你还得多写个线程定时访问网站的一个空白.aspx的网页。
winner2050 2009-09-07
  • 打赏
  • 举报
回复

你用的是access数据库,数据库链接你肯定是Server.MapPath(相对路径)

在多线程下,Server.MapPath被非web线程使用就导致异常,循环就停止了。

换一下。

/// <summary>
/// 获得绝对路径
/// </summary>
/// <param name="strPath">相对路径</param>
/// <returns>绝对路径</returns>
public static string MapPath(string strPath)
{
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(strPath);
}
else //非web程序引用
{
strPath = strPath.Replace("/", "\\");
if (strPath.StartsWith("\\"))
{
strPath = strPath.TrimStart('\\');
}
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
}
}
LutzMark 2009-09-07
  • 打赏
  • 举报
回复
用System.Threading.Timer
ChargeForward 2009-09-07
  • 打赏
  • 举报
回复
貌似和你的deleSQF 委托有关系 不太清楚 帮顶
yxzbest 2009-09-07
  • 打赏
  • 举报
回复
顶,关注……
cpp2017 2009-09-07
  • 打赏
  • 举报
回复
像这种定时做的事,最好用winform来做,
happyboyxq1985 2009-09-07
  • 打赏
  • 举报
回复
.net线程间的调用是要通过委托的啊。
cpp2017 2009-09-07
  • 打赏
  • 举报
回复
如果你的这个发送短信遇到异常,这个循环就不会再进行了.
ttf1 2009-09-07
  • 打赏
  • 举报
回复
在线程里不执行委托回调getSMSContent方法

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧