Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6221853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:12:28+00:00 2026-05-24T08:12:28+00:00

I like to send a mail using a task. This is my code to

  • 0

I like to send a mail using a task. This is my code to envoke the SendMail method:

Task.Factory.StartNew(() => SendMail(toTextBox.Text,
                                     subjectTextbox.Text,
                                     BodyEditor.ContentHtml,
                                     filenames.ToArray(),
                                     ccTextBox.Text,
                                     bccTextBox.Text));

Problem is: I can’t pass UI information (textbox etc..) like this because the objects are owned by another thread.

Error message: The calling thread cannot access this object because a different thread owns it.

How should I pass these parameters to my action?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:12:31+00:00Added an answer on May 24, 2026 at 8:12 am

    Are you already in your UI thread? If so, you merely need to extract the properties first, so that when your lambda expression is called, you’ve already performed the property access.

    For example:

    string recipient = toTextBox.Text;
    string subject = subjectTextBox.Text;
    string html = BodyEditor.ContentHtml;
    string cc = ccTextBox.Text;
    string bcc = bcTextBox.Text();
    
    Task.Factory.StartNew(() => SendMail(recipient,
                                         subject,
                                         html,
                                         filenames.ToArray(),
                                         cc,
                                         bcc));
    

    The variables will be captured by the lambda expression – basically the compiler will generate an extra class for you which squirrels away the information, so that when the lambda expression is evaluated, it’s still available.

    If this code is executing not in the UI thread, then you need to basically wrap the whole thing in another action, to execute it in the UI thread:

    Action action = () {
        string recipient = toTextBox.Text;
        string subject = subjectTextBox.Text;
        string html = BodyEditor.ContentHtml;
        string cc = ccTextBox.Text;
        string bcc = bcTextBox.Text();
    
        Task.Factory.StartNew(() => SendMail(recipient,
                                             subject,
                                             html,
                                             filenames.ToArray(),
                                             cc,
                                             bcc));
    };
    
    // Or Dispatcher.BeginInvoke(action); for WPF/Silverlight.
    BeginInvoke(action);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a Rails application and would like to send/receive text messages through my
i am using Interop.Domino.dll and able to send mail via c# code to lotus
I want to send HTML-emails, using Django templates like this: <html> <body> hello <strong>{{username}}</strong>
I'm trying to send an e-mail with Codeigniter like this: $this->load->library('email'); $this->email->from(myemail@email.com); $this->email->reply_to(myemail@email.com); $this->email->to(myemail@email.com);
I would like to send e-mail from my own custom webpart using SPUtility (on
I've been using this gist to send an email after deployment but I'd like
I'm using devise with rails3.1 Now I'd like devise to send confirmation mail after
I would like to send mail from a script on a Windows Server 2003
How often do we see stuff like Send this page to a friend on
I'd like to send an email for each row of a result set using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.