Unique GUID for an asynchronous task

A

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);


}
 
J

Jeff Johnson

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.
 
A

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.
 
J

Jeff Johnson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top