0

I read a code to send a email,here part of it:

MailUserState state = new MailUserState()
{
    AutoReleaseSmtp = m_autoDisposeSmtp,
    CurMailMessage = mMailMessage,
    CurSmtpClient = m_SmtpClient,
    IsSmpleMail = true,
    UserState = AsycUserState,
};
if (m_autoDisposeSmtp)
  m_SmtpClient = null;

ThreadPool.QueueUserWorkItem((userState) =>
{
    MailUserState curUserState = userState as MailUserState;
    curUserState.CurSmtpClient.SendAsync(mMailMessage, userState);
}, state);

Why the lambda expression has no return value?

I think it shoud return a callback instance object.But it has no return statement.Why?

7
  • Where would it return the value to? What would the thread pool do with the return value? Commented Dec 7, 2013 at 8:49
  • The QueueUserWorkItem function has two parameters. Commented Dec 7, 2013 at 8:50
  • 1
    The lambda expression is a WaitCallback instance (or convertible to one, anyway). It doesn't need to return one. Commented Dec 7, 2013 at 8:50
  • I don't known too much about thread Commented Dec 7, 2013 at 8:50
  • @Dolphin, the QueueUserWorkItem function has two overloads, one of which takes two parameters. Commented Dec 7, 2013 at 8:51

1 Answer 1

7

Why the lambda expression has no return value?

The first argument of the ThreadPool.QueueUserWorkItem method is a WaitCallback delegate which looks like this:

public delegate void WaitCallback(object state)

So as you can see it is a function taking one object parameter and having no return value. And that's exactly what the lambda expression in your code is. The QueueUserWorkItem method will draw a thread from the thread pool if/when available and it will execute the code in the callback on this thread. There is no return value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.