Ibraheem Osama Mohamed (ابراهيم اسامه محمد)

Welcome to my blog :)

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

with one comment

invalidOperationExecption was unhandeled
The calling thread cannot access this object because a different thread owns it. (Windows Presentation Foundation and Windows Phone7)
This error occurs when you try to access a UI object from another thread ex: you are defined a SerialPort and in the receive event want to write the data into a textbox.
The solution for this exception is to use dispatcher which  managing the queue of work items for a thread.
lets code 🙂
1)Add new WPF of WP7  then add a textbox and a button
2)Define a thread then try to change the textbox text from the ThreadStart function
3)This error will occur
4)Define a Delegate and a function to change the textbox to any text you want

delegate voidSetTextBoxTextDelegate(TextBox txtblk, string text);

        void SetTextBlockText(TextBox textbox, string text)

        {

            textbox.Text = text;

        }
5)In the Thread function add a dispatcher for the textbox

publicvoid func()

        {

            textbox1.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Low, () => { textbox1.Text = “Hello World”; });

        }

6)Now the Code will Run (This is the full code)

privateThread thread;

        public MainWindow()

        {

            InitializeComponent();

            ThreadStart s = newThreadStart(func);

            thread = newThread(s);

            this.Closed += (a, b) =>

            {

                thread.Abort();

            };

        }

        delegatevoidSetTextBoxTextDelegate(TextBox txtblk, string text);

        void SetTextBlockText(TextBox textbox, string text)

        {

            textbox.Text = text;

        }

        protectedvoid func()

        {

            textbox1.Dispatcher.BeginInvoke(new

                    SetTextBoxTextDelegate(SetTextBlockText),

                                                textbox1, “Hello Wolrd”);

        }

        privatevoid Button_Click_1(object sender, RoutedEventArgs e)

        {

            thread.Start();

        }

Ibraheem Osama Mohamed
@IbraheemOsamaMo

Advertisements

Written by Ibraheem Osama Mohamed

June 24, 2012 at 9:48 pm

Posted in C#, Silverlight, WP7, wpf

One Response

Subscribe to comments with RSS.

  1. […] to change the text of a TextBox in a task (If you have the same problem in WPF or in WP7 check this post) This post will help you to solve the problem and will help if you are developing Metro Apps Lets […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: