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

Welcome to my blog :)

The application called an interface that was marshalled for a different thread.

with 3 comments

This error Occur when you try to access an object in the UI from another task or thread in Windows 8 like if you are trying 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 start
1)Add new empty Metro Style App.
2)Add a Button and a TextBox and assign a click event for the Button.
3)Declare a Task and Initialize it , initialize an action for the task and write a function for the action.

this.Loaded += (A, b) =>

{

Action a = newAction(func);

task = new System.Threading.Tasks.Task(a);

};

public void func()

{

textbox1.Text = “Hello World”; //this code will not run this code is just for learning (Wrong Code)

}

4)In the Button Click Event start the task

task.Start();

5)Run the Code and in the Click event you’ll get this error.The solution for this error is to call a Dispatcher for the UI element and invoke the RunAsync method which take 2 args CoreDispatcherPriority(High,Medium,Low) and a Delegate.

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

Here is the Complete Code 🙂

System.Threading.Tasks.Task task;

   publicvoid func()

        {

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

        }

        public MainPage()

        {

            this.InitializeComponent();

            this.Loaded += (A, b) =>

            {

                Action a = newAction(func);

                task = new System.Threading.Tasks.Task(a);

            };

        }

        protectedoverridevoid OnNavigatedTo(NavigationEventArgs e)

        {

        }

        privatevoid Button_Click_1(object sender, RoutedEventArgs e)

        {

            task.Start();

        }

I hope that you get the point 🙂

Ibraheem Osama Mohamed
@IbraheemOsamaMo

Advertisements

Written by Ibraheem Osama Mohamed

June 24, 2012 at 10:29 pm

3 Responses

Subscribe to comments with RSS.

  1. Hi, i believe that i saw you visited my web site so i came to “return the prefer”.I’m trying to to find issues to enhance my site!I assume its good enough to make use of a few of your ideas!!

    pomegranate heart

    July 10, 2012 at 10:28 pm

  2. Great ! Thanks waiting for more ……

    Zooka Mohammed

    July 25, 2012 at 2:03 am

  3. This is really helpful!! Saved me a lot of time! It’s not just Indian guys who actually understand programming – awesome!

    Fedor Andreev

    June 13, 2013 at 12:58 am


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: