Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
first
public Thread mailthread;

Second I call this thread like this

C#
Mailthread = new Thread(() => sendmail());
            Mailthread.Start();




but it returns error :

"The calling thread cannot access this object because a different thread owns it"

Notice i am useing other thread in same way in the form and it works
Posted

1 solution

If you are in Windows Forms, WPF or Silverlight you need to get back onto the same thread to access the data. My guess is that your sendmail() method tries to access a textblock with the information for the mail message.

What you should do is wrap the message information up into a class and pass it into a ParamaterizedThreadStart as the parameter.
This way you won't need to call controls to get the information later.

However should you have to access the controls directly:
WindowsForms:
Control.Invoke -> this will get you back on the thread

WPF
UiElemenet.Dispatcher.Invoke -> gets you back on the thread

Silverlight
App.Current.RootVisual.Dispatcher.Invoke -> gets you back on the main thread
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900