异步调用,异步处理

首先在类中定义代理:

private delegate bool AsyncRegistPhone(string state);
private delegate void AsyncSendMail(string addr, string title, string content);
private delegate int AsyncUploadFile(string path, byte[] data, long sendedsize, long length);


异步封装类(作为嵌套类使用):

class SendMailClass
{
private string mailaddr;
private string mailtitle;
private string mailcontent;
public SendMailClass(string addr, string title, string content)
{
mailaddr = addr;
mailtitle = title;
mailcontent = content;
ThreadPool.QueueUserWorkItem(new WaitCallback(DoSendThread), this);
}

private void DoSendThread(object obj)
{
SendMailClass dd = obj as SendMailClass;
MailBLL mbll = new MailBLL();
mbll.SendMail(dd.mailaddr, dd.mailtitle, dd.mailcontent);
}
}
class UploadFileClass
{
private EnterpiseUserInfo fileuserInfo;
private Image fileimage;
private string filepath;
private byte[] filedata;
private long filelength;
public UploadFileClass(EnterpiseUserInfo userinfo, Image image, string path, byte[] data, long length)
{
fileuserInfo = userinfo;
fileimage = image;
filepath = path;
filedata = data;
filelength = length;
FileBLL fileBll = new FileBLL();
AsyncUploadFile auf = new AsyncUploadFile(fileBll.Upload);
auf.BeginInvoke(filepath, filedata, 0, filelength, UploadFileCallBack, auf);
}

/// <summary>
/// 文件上传
/// </summary>
/// <param name="ar"></param>
private void UploadFileCallBack(IAsyncResult ar)
{
AsyncUploadFile dlgt = ar.AsyncState as AsyncUploadFile;
try
{
int result = dlgt.EndInvoke(ar);
if (result >= 0)
{
fileuserInfo.UserGravatar = filepath;

fileimage.ImageUrl = "Image.ashx?file=" + filepath;
}
else
fileuserInfo.UserGravatar = "";
}
catch
{
}
}
}
/// <summary>
/// 注册话机回调函数
/// </summary>
/// <param name="ar"></param>
private static void CallBack(IAsyncResult ar)
{
AsyncRegistPhone dlgt = ar.AsyncState as AsyncRegistPhone;
try
{
dlgt.EndInvoke(ar);
}
catch
{
}
}

程序中调用:

protected bool SaveUser()
{
#region 保存员工
EnterpiseInfo info = (EnterpiseInfo)Session["Info"];
//员工信息
if (Session["Info"] == null)
{
ClientScript.RegisterStartupScript(this.GetType(), "n1", "refash();", true);
return false;
}
string userName = this.txtUserName.Text;
string userPWD = this.txtUserPWD.Text;
string userGender = this.ddlUserGender.SelectedValue;
string depID = this.ddlDepartment.SelectedValue;

string userPosition = this.ddlPosition.SelectedValue;
string roleID = this.ddlRole.SelectedValue;
string userMobile = this.txtUserMobile.Text;
string userPhone = this.txtUserPhone.Text;

string userGravatar = "";
byte[] data = null;
if (FileUploadIMG.HasFile)
{
if (FileUploadIMG.FileName.IndexOf(".jpg") != -1
|| FileUploadIMG.FileName.IndexOf(".png") != -1
|| FileUploadIMG.FileName.IndexOf(".gif") != -1
|| FileUploadIMG.FileName.IndexOf(".jpge") != -1
)
{
userGravatar = info.EnterpiseID + "/Avator/" + Guid.NewGuid().ToString() + "." + FileUploadIMG.FileName.Split('.')[1];
data = FileUploadIMG.FileBytes;
}
}
string mailPWD = this.txtMailPWD.Text;
string uEmailAddress = this.txtUEmailAddress.Value;
string uEmailSign = this.txtUEmailSign.Text;
int uEmailISBack = 1;
string uEmailCircle = "60";
int uEmailIsUesd = 1;

pop3Address = this.txtPop3Address.Text;
smtpAddress = this.txtSmtpAddress.Text;

EnterpiseUserBLL entUser = new EnterpiseUserBLL();
UserEmailBLL email = new UserEmailBLL();

string type = Request.QueryString["type"];
string entUserID = Request.QueryString["entUserID"];
if (type == "modify")
{
//编辑
int iEntUserID = Convert.ToInt32(entUserID);
if (iEntUserID > 0 && IsMailExistExceptSelf(iEntUserID))//再次判断修改后邮件地址是否存在
{
lblEntUserMsg.Text = "邮件地址已经存在!";
return false;
}
EnterpiseUserInfo userInfo = entUser.Get(int.Parse(entUserID));
if (userInfo != null)
{
userInfo.UserName = userName;
userInfo.UserPWD = userPWD;
userInfo.UserGender = int.Parse(userGender);
userInfo.DepID = int.Parse(depID);
userInfo.UserPosition = userPosition;
userInfo.RoleID = int.Parse(roleID);

userInfo.UserMobile = userMobile;
userInfo.UserPhone = userPhone;

if (!string.IsNullOrEmpty(userGravatar))
{
UploadFileClass ufc = new UploadFileClass(userInfo, imgUserGravatar, userGravatar, data, data.LongLength);
}
bool flg = entUser.Update(userInfo);
if (flg)
{
//更新邮箱
UserEmailInfo emailInfo = email.GetInfo(userInfo.EnterpiseUserID);
if (emailInfo != null)
{
emailInfo.UEmailAddress = uEmailAddress;
emailInfo.UEmailName = userName;//
emailInfo.UEmailPwd = mailPWD;
emailInfo.UEmailISBack = uEmailISBack;
emailInfo.UEmailCircle = int.Parse(uEmailCircle);
emailInfo.UEmailIsUsed = uEmailIsUesd;
if (String.IsNullOrEmpty(pop3Address) && String.IsNullOrEmpty(smtpAddress))
{
emailInfo.Pop3Address = pop3Address;
emailInfo.SmtpAddress = smtpAddress;
}
emailInfo.UEmailSign = uEmailSign;

emailInfo.UEmailModifyTime = DateTime.Now;


if (chkPopSsl.Checked)
{
emailInfo.IsPopSsl = 1;
emailInfo.popPort = 993;
}
else
{
emailInfo.IsPopSsl = 0;
emailInfo.popPort = 143;//默认开启143,没有开启为110;pop端口与imap端口公用;
}
if (chkSmtp.Checked)
{
emailInfo.IsSmtpSsl = 1;
emailInfo.smtpPort = 465;
}
else
{
emailInfo.IsSmtpSsl = 0;
emailInfo.smtpPort = 25;
}

emailInfo.IsOpenImap = 1;

email.Update(emailInfo);
this.lblEntUserMsg.Text = "保存成功";

//刷新组织架构
ClientScript.RegisterStartupScript(this.GetType(), "n1", "refash();", true);
return true;
}
else
{
this.lblEntUserMsg.Text = "邮件信息已被删除,不能修改!";
return false;
}
}
else
{
this.lblEntUserMsg.Text = "保存失败";
return false;
}
}
else
{
this.lblEntUserMsg.Text = "保存失败";
return false;
}
}
else //添加操作
{
if (IsMailExist())//再次判断邮件地址是否存在
return false;
//第一步 添加用户
EnterpiseUserInfo userInfo = new EnterpiseUserInfo();

userInfo.EnterpiseID = info.EnterpiseID;
userInfo.UserName = userName;
userInfo.UserPWD = userPWD;
userInfo.UserGender = int.Parse(userGender);
userInfo.DepID = int.Parse(depID);
userInfo.UserPosition = userPosition;
userInfo.RoleID = int.Parse(roleID);
userInfo.UserMobile = userMobile;
userInfo.UserPhone = userPhone;
userInfo.UserGravatar = userGravatar;
userInfo.UserAddTime = DateTime.Now;
userInfo.UserModifyTime = DateTime.Now;
if (!string.IsNullOrEmpty(userGravatar))
{
UploadFileClass ufc = new UploadFileClass(userInfo, imgUserGravatar, userGravatar, data, data.LongLength);
}

//第二步 设置话机号
EnterpiseUserBLL eubll = new EnterpiseUserBLL();
NOResourceInfo nrinfo = new NOResourceBLL().GetPhoneNO();
string userPhoneNO = nrinfo.Number;
AsyncRegistPhone dlgt = new AsyncRegistPhone(eubll.RegistPhone);
IAsyncResult ar = dlgt.BeginInvoke(userPhoneNO, null, null);//异步调用

//话机号赋予用户,保存用户
userInfo.UserPhoneNO = userPhoneNO;
int userID = entUser.GetIdentityNO(userInfo);
//第三步 添加邮箱
if (userID > 0)
{
UserEmailInfo emailInfo = new UserEmailInfo();
emailInfo.EnterpiseUserID = userID;
emailInfo.UEmailAddress = uEmailAddress;
emailInfo.UEmailName = userName;
emailInfo.UEmailPwd = mailPWD;
emailInfo.UEmailISBack = uEmailISBack;
emailInfo.UEmailCircle = int.Parse(uEmailCircle);
emailInfo.UEmailIsUsed = uEmailIsUesd;

if (!String.IsNullOrEmpty(pop3Address) && !String.IsNullOrEmpty(smtpAddress))
{
emailInfo.Pop3Address = pop3Address;
emailInfo.SmtpAddress = smtpAddress;
}
else
{
//取邮件默认配置
MailDoMainConfigBLL mdcbll = new MailDoMainConfigBLL();
MailDoMainConfigInfo mdcinfo = mdcbll.GetByDomain(uEmailAddress.Substring(uEmailAddress.IndexOf('@') + 1));
if (mdcinfo != null)
{
if (ddlPopProtocol.SelectedValue.Equals("1"))
{
emailInfo.Pop3Address = mdcinfo.ImapHost;
}
else
{
emailInfo.Pop3Address = mdcinfo.PopHost;
}
emailInfo.SmtpAddress = mdcinfo.SmtpHost;
}
}
if (chkPopSsl.Checked)
{
emailInfo.IsPopSsl = 1;
emailInfo.popPort = 993;
}
else
{
emailInfo.IsPopSsl = 0;
emailInfo.popPort = 143;//默认开启143,没有开启为110;pop端口与imap端口公用;
}
if (chkSmtp.Checked)
{
emailInfo.IsSmtpSsl = 1;
emailInfo.smtpPort = 465;
}
else
{
emailInfo.IsSmtpSsl = 0;
emailInfo.smtpPort = 25;
}
if (ddlPopProtocol.SelectedValue.Equals("1"))//接收协议为IMap
{
emailInfo.IsOpenImap = 1;
}
else
{
emailInfo.IsOpenImap = 0;
}
emailInfo.UEmailSign = uEmailSign;
emailInfo.UEmailCreateTime = DateTime.Now;
emailInfo.UEmailModifyTime = DateTime.Now;
if (email.Add(emailInfo))
{
//第四步 发送注册员工成功信息
MailBLL mbll = new MailBLL();
StringBuilder strContent = new StringBuilder();
strContent.Append("<div style=\"border:0px; border-color: Black;font-size: 10px; line-height:16px;\">");
strContent.Append("<div style=\"width: 453px; height: 245px; padding-left:60px; padding-top:17px;\">");
strContent.Append("<span>尊敬的用户,您好:欢迎贵公司使用xx,您的账号已开通!<br /><br />");
strContent.Append("请使用以下账号和密码登录:<br />账户:");
strContent.Append(uEmailAddress);
strContent.Append("<br />登录密码:");
strContent.Append(this.txtUserPWD.Text);
strContent.Append("<br />xx网址:http://www.panoview.cn<br /><br /><br /><br />");
strContent.Append("xx运营中心 2011年8月5日<br />");
strContent.Append("----------------------------------------------------------------------------------------------------<br />");
strContent.Append("此邮件由系统发出,请勿直接回复。 </span>");
strContent.Append("</div><div style=\"position:relative; top:-160px; left:320px; width:98px;\">");
strContent.Append("<a href=\"http://www.panoview.cn\">下载xx客户端</a></div></div>");

//mbll.SendMail(uEmailAddress, "员工注册信息--xx", strContent.ToString());
ar.AsyncWaitHandle.WaitOne();
if (dlgt.EndInvoke(ar))
{
SendMailClass sendmail = new SendMailClass(uEmailAddress, "员工注册信息--xx", strContent.ToString());
}

else
{
//回滚操作
this.lblEntUserMsg.Text = "话机注册失败!";
//email.DeleteByUserID(userID);//这里根据用户标识号删除邮箱,一个用户对应一个邮箱
entUser.Delete(userID);
return false;
}

}
this.lblEntUserMsg.Text = "保存成功";
return true;
}
else
{
this.lblEntUserMsg.Text = "保存失败";
return false;
}
}
#endregion
}

以上提供了注册话机(即网上电话注册), 发送邮件异步调用和上传文件异步调用

仅供参考,无法运行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值