I have two methods on the page. One AddMessageToInboxOutbox(params[]) and other one SendNewMessageMail(params[]).
Once user sends an message to other other first message is added to DB and then sent to recipient’s email. Sometimes SMTP is heavy loaded and it takes up to 5 seconds to get answer from it. I want to enclose SendNewMessageMail(params[]) with async call using other thread or something. I have never done that.
How do i perform this action right in ASP.NET?
You can use a delegate to call the method asynchronously:
This will use the
ThreadPoolto assign another thread. In my example, I’m also not registering a callback. Note that this callback, if specified, will be placed onto an unknownThreadPoolthread. CallingEndInvokeon the delegate referencedelwill block until it completes.EndInvoketakes theIAsyncResultobject that is returned from theBeginInvokemethod, if you do this, keep references to thedelandIAsyncResult. Also, this isn’t specific to ASP.NET.Note: multi-threading is a powerful mechanism and an in-depth subject to learn. Coupling multi-threading with ASP.NET is arguably more involved than, say, a WinForms application – due to the page lifecycle etc. I’d advise reading up on the multi-threading topic as a whole too.