Hades0103
Hades0103

Reputation: 3

WPF component InvalidOperationException

I have trouble with getting the text value of WPF TextBox.

img

There are many same exception list(InvalidOperationException) on textbox.
Why does these exception occur? To remove these exceptions , what should I do?

These are code snippets.

public partial class MainPage : Page, 
                                Autodesk.Revit.UI.IDockablePaneProvider    
{
       ...
     private static BackgroundWorker workerTimers = new BackgroundWorker();

}

public sync void LoadPage()
{
       ...
     workerTimers.DoWork += workerTimers_DoWork;
       ...
}

async void workerTimers_DoWork(object sender, DoWorkEventArgs e)
{
       ...
    SearchText = UserSearchTextBox.Text;
       ...
}

Upvotes: 0

Views: 144

Answers (1)

Brannon
Brannon

Reputation: 5414

You can't utilize WPF controls from a background thread. You need to access them from the main UI thread. Use the Dispatcher property on the control:

UserSearchTextBox.Dispatcher.Invoke(() => SearchText = UserSearchTextBox.Text);

Are you expecting the data to change while the background thread is running? If not, grab it before you start the background thread.

Upvotes: 1

Related Questions