Home All Groups Group Topic Archive Search About

Unique GUID for an asynchronous task

Author
23 Jun 2009 6:39 PM
AA2e72E
I am using  System.Net.Mail to send mail asynchronously. I have an event
handler that gets trigerred when the task is completed.

One of the arguments of the event handler has a method UserState; UserState
gets a unique GUID for the asynchronous task.

How do I get this GUID when (or before) I send the mail? I'd like to record
the GUID in a database table and then from the event handler, update the row
with the outcome reported by the argument e.

Thanks.

SmtpClient client = new SmtpClient(mailClient);

client.SendCompleted += new SendCompletedEventHandler(MailDeliveryComplete);

public void MailDeliveryComplete(object sender,
System.ComponentModel.AsyncCompletedEventArgs e)
        {
            LogMessage("Message token = " + e.UserState);


        }

Author
23 Jun 2009 8:31 PM
AA2e72E
More complete code of the context is found here
http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx

Show quoteHide quote
"AA2e72E" wrote:

> I am using  System.Net.Mail to send mail asynchronously. I have an event
> handler that gets trigerred when the task is completed.
>
> One of the arguments of the event handler has a method UserState; UserState
> gets a unique GUID for the asynchronous task.
>
> How do I get this GUID when (or before) I send the mail? I'd like to record
> the GUID in a database table and then from the event handler, update the row
> with the outcome reported by the argument e.
>
> Thanks.
>
> SmtpClient client = new SmtpClient(mailClient);
>
> client.SendCompleted += new SendCompletedEventHandler(MailDeliveryComplete);
>
> public void MailDeliveryComplete(object sender,
> System.ComponentModel.AsyncCompletedEventArgs e)
>         {
>             LogMessage("Message token = " + e.UserState);
>            
>
>         }
>        
>
Are all your drivers up to date? click for free checkup

Author
23 Jun 2009 8:46 PM
Jeff Johnson
Show quote Hide quote
"AA2e72E" <AA2e***@discussions.microsoft.com> wrote in message
news:1CA5C82E-F25C-4783-AC2A-61D972856704@microsoft.com...

>I am using  System.Net.Mail to send mail asynchronously. I have an event
> handler that gets trigerred when the task is completed.
>
> One of the arguments of the event handler has a method UserState;
> UserState
> gets a unique GUID for the asynchronous task.
>
> How do I get this GUID when (or before) I send the mail? I'd like to
> record
> the GUID in a database table and then from the event handler, update the
> row
> with the outcome reported by the argument e.
>
> Thanks.
>
> SmtpClient client = new SmtpClient(mailClient);
>
> client.SendCompleted += new
> SendCompletedEventHandler(MailDeliveryComplete);
>
> public void MailDeliveryComplete(object sender,
> System.ComponentModel.AsyncCompletedEventArgs e)
>        {
>            LogMessage("Message token = " + e.UserState);
>
>
> }

Assuming you're using the SendAsync() method, you pass this information in
the userToken parameter. If you need to pass more than one piece of
information, create a class and add your various items as properties, then
pass the class. In the event handler, you cast the UserState property back
into your class object.
Author
23 Jun 2009 9:31 PM
AA2e72E
Thanks for the response Jeff. I think I understand (most) of what you are
saying:

- I am using SendAsync; userToken is the second argument of SendAsync.
- I understand about creating a class and adding multiple properties
- I understand about passsing the class as the userToken argument.

However, I am not totally clear about "In the event handler, you cast the
UserState property back  into your class object. "  I guess you might mean
the following: have one of the properties of the class such that it uniquely
identifies the message being sent and then use the same property to update
the row in the database.

Casting the UserState:

myClass mailid = (myClass) e.UserState;

Then ,mailid.myproperty will return me what I passed?

I suppose I can try out the code. But, if I have completely misunderstood,
please enlighten me ... with some code snippets (?).  Thanks.



Show quoteHide quote
"Jeff Johnson" wrote:

> "AA2e72E" <AA2e***@discussions.microsoft.com> wrote in message
> news:1CA5C82E-F25C-4783-AC2A-61D972856704@microsoft.com...
>
> >I am using  System.Net.Mail to send mail asynchronously. I have an event
> > handler that gets trigerred when the task is completed.
> >
> > One of the arguments of the event handler has a method UserState;
> > UserState
> > gets a unique GUID for the asynchronous task.
> >
> > How do I get this GUID when (or before) I send the mail? I'd like to
> > record
> > the GUID in a database table and then from the event handler, update the
> > row
> > with the outcome reported by the argument e.
> >
> > Thanks.
> >
> > SmtpClient client = new SmtpClient(mailClient);
> >
> > client.SendCompleted += new
> > SendCompletedEventHandler(MailDeliveryComplete);
> >
> > public void MailDeliveryComplete(object sender,
> > System.ComponentModel.AsyncCompletedEventArgs e)
> >        {
> >            LogMessage("Message token = " + e.UserState);
> >
> >
> > }
>
> Assuming you're using the SendAsync() method, you pass this information in
> the userToken parameter. If you need to pass more than one piece of
> information, create a class and add your various items as properties, then
> pass the class. In the event handler, you cast the UserState property back
> into your class object.
>
>
>
Author
24 Jun 2009 1:19 PM
Jeff Johnson
Show quote Hide quote
"AA2e72E" <AA2e***@discussions.microsoft.com> wrote in message
news:2A31E4B0-7C1F-4F3C-80B6-F4DAEB5E27D5@microsoft.com...

> However, I am not totally clear about "In the event handler, you cast the
> UserState property back  into your class object. "  I guess you might mean
> the following: have one of the properties of the class such that it
> uniquely
> identifies the message being sent and then use the same property to update
> the row in the database.
>
> Casting the UserState:
>
> myClass mailid = (myClass) e.UserState;
>
> Then ,mailid.myproperty will return me what I passed?
>
> I suppose I can try out the code. But, if I have completely misunderstood,
> please enlighten me ... with some code snippets (?).  Thanks.

You've already discovered the answer, but for posterity: You understood
perfectly. That's exactly what you do.
Author
24 Jun 2009 6:58 AM
AA2e72E
Jeff, thanks for the guidance/pointers. All works fine.

Show quoteHide quote
"Jeff Johnson" wrote:

> "AA2e72E" <AA2e***@discussions.microsoft.com> wrote in message
> news:1CA5C82E-F25C-4783-AC2A-61D972856704@microsoft.com...
>
> >I am using  System.Net.Mail to send mail asynchronously. I have an event
> > handler that gets trigerred when the task is completed.
> >
> > One of the arguments of the event handler has a method UserState;
> > UserState
> > gets a unique GUID for the asynchronous task.
> >
> > How do I get this GUID when (or before) I send the mail? I'd like to
> > record
> > the GUID in a database table and then from the event handler, update the
> > row
> > with the outcome reported by the argument e.
> >
> > Thanks.
> >
> > SmtpClient client = new SmtpClient(mailClient);
> >
> > client.SendCompleted += new
> > SendCompletedEventHandler(MailDeliveryComplete);
> >
> > public void MailDeliveryComplete(object sender,
> > System.ComponentModel.AsyncCompletedEventArgs e)
> >        {
> >            LogMessage("Message token = " + e.UserState);
> >
> >
> > }
>
> Assuming you're using the SendAsync() method, you pass this information in
> the userToken parameter. If you need to pass more than one piece of
> information, create a class and add your various items as properties, then
> pass the class. In the event handler, you cast the UserState property back
> into your class object.
>
>
>

Bookmark and Share