0

I'm creating a DelayedTextbox. I did this by extending the TextBox and adding dependency properties (thanks to help on another question)

So I have a cs file that looks like this:

public class DelayedTextBox : TextBox
{
   public int Delay
    {
        get
        {
            return (int)GetValue(DelayProperty);
        }
        set
        {
            SetValue(DelayProperty, value);
        }
    }

    public static readonly DependencyProperty DelayProperty =
        DependencyProperty.Register(
            "Delay",
            typeof(int),
            typeof(DelayedTextBox),
            new PropertyMetadata(300));
}

Then in my xaml file I attempt to use the new control:

<h:DelayedTextBox Delay="300" />

Where Delay is one of the dependency properties.

When I run the code though, the first time DelayedTextBox.cs tries to access Delay it throws the following error:

System.Exception: 'The application called an interface that was marshalled for >a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))'

How can I properly extend the Textbox with my custom control?

JT Smith
  • 361
  • 1
  • 2
  • 15
  • can you show the code of DelayedTextBox ? – Dave Smits Mar 10 '18 at 19:49
  • Added code for DelayedTextBox – JT Smith Mar 10 '18 at 21:34
  • 1
    My psychic debugger says you spawn a background thread (or start a new `Task`), wait for some time, then try to set a TextBox property. You need to set all properties from the UI thread. – Peter Torr - MSFT Mar 10 '18 at 21:56
  • Where can I get one of these psychic debuggers? :) Yes, I use the Timer object (from System.Threading) which I start/update on the TextChanged event. When the Timer is up I call a method in DelayedTextbox to do some stuff. How would I force these to run in the UI thread? – JT Smith Mar 10 '18 at 22:02
  • Found the answer on this question: https://stackoverflow.com/questions/10579027/run-code-on-ui-thread-in-winrt. Thanks Peter for the point in the right direction – JT Smith Mar 10 '18 at 22:29

0 Answers0