c# asp.net call async method from static sync -
i call static method named - validsendpass
ajax
[webmethod] public static string validsendpass(string em)
in method need create email class , call method send async email
classes.email ems = new classes.email(); ems.sendpassword(myemail, email name, user email, user name, password, "your password", pas);
this method:
var fromaddress = new mailaddress(fromem, fromname); var toaddress = new mailaddress(toem, toname); var smtp = new smtpclient { host = "smtp.gmail.com", port = 587, enablessl = true, deliverymethod = smtpdeliverymethod.network, usedefaultcredentials = false, credentials = new networkcredential(fromaddress.address, frompassword), timeout = 20000 }; using (var message = new mailmessage(fromaddress, toaddress) { subject = subject, body = body }) { try { smtp.send(message); } catch { } }
for replace smtp.send smtp.sendmailasync or smtp sendasync, add async.
how need change code - call async method static method?
you can't. you're asking how synchronously call asynchronous method. if make synchronous it no longer asynchronous.
you can block method until asynchronous operation has completed, if you'll have defeated entire purpose of making method asynchronous in first place.
if need end result synchronous, leave synchronous top bottom, , don't use asynchronous methods anywhere. if you're going use asynchrony need make everything asynchronous, way up, there benefits.
Comments
Post a Comment