Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Sending Email Asynchronously
Message
From
18/01/2013 12:21:37
 
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01563134
Message ID:
01563284
Views:
48
>>>Think my last post was correct. Maybe remove the using when creating the SmtpClient and put 'client.Dispose()' in the delegate callback. i.e in outline:
SmtpClient client = new SmtpClient();
>>>                    {
>>>                        client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
>>>                            try
>>>                            {
>>>                                 client.SendCompleted += delegate
>>>                                {
>>>                                  client.Dispose();
>>>                                    lock (numRemainingLock)
>>>                                    {
>>>                                        if (--numRemaining == 0)
>>>                                        {
>>>                                            waitHandle.Set();
>>>                                        }
>>>                                    }
>>>                                };
>>>                                client.SendAsync(msg, userState);                 // Send our email.   
>>>                            }
>>>                            catch (Exception)
>>>                            {
>>>                                msg.Dispose();
>>>                                // throw;
>>>                            }
>>>                        }
>>>                    }
>>
>>Superb! Thanks once again Viv. What is interesting is that all the email is now being sent, but the code never hits the SendCompletedCallback.
>>
>>Oh, it also does not seem to be running asynchronously as I don't get control back in the UI for quite a while.
>>
>>One of these days I'll understand C# :(
>
>Your 'waitHandle.WaitOne();' will stop the PostAWBS() method from returning until all emails have been sent even though each individual instance of SmtpClient is sending asynchronously. Whilst this should be faster than using the regular client.Send() to make it truly asynchronous from a UI POV you would need to make the PostAWBS() method itself into an async call. How you do that best would depend on which version of .NET you are using......
>
>Update: your second eventhandler is probably not firing because it is defined as static and you are calling an instance. Try without the static.....

I am using .NET 4.0

When I commented out the waitHandle line, I then got the breakpoint in SendCompletedCallback to fire.
Frank.

Frank Cazabon
Samaan Systems Ltd.
www.samaansystems.com
Previous
Reply
Map
View

Click here to load this message in the networking platform