Monday, November 28, 2011

Myth Self Note 10: C# WPF - Update UI in other Thread


Problem:
1 - Error Message: "The calling thread cannot access this object because a different thread owns it"
2 - Can't add value or edit listbox while using thread

Search Key:
1 - listbox c# thread used cannot access

Solution:
Use "Dispatcher.Invoke"

'Myth' Example:
1- this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
{
//do something here
}));

Screenshot Example:
2 - call the function from the running thread
"Thread t = new Thread(startScan);
t.Start();"

private void startScan()
{
bla bla bla
ProgressChanged(filesPath);
bla bla bla
}

Help Link:

0 comments: