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?
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:
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: