Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I currently run into the problem to update WPF controls from a worker thread.
After searching on Google for hours I hope to get an answer here.

Situation:
- I do have a GUI
- The GUI has a viewmodel attached to it
- The videmodel contains all properties for the binding. This contains int, float, bool and string property values as well as an int[] array.
- All properties look like this:
C#
private volatile string _InputTypeString = "";
public string InputTypeString
{
    get { return _InputTypeString; }
    set { _InputTypeString = value; OnPropertyChanged("InputTypeString"); }
}

- All of the controls are bound to the viewmodel via XML bindings
- When I do not use the GUI updates, my Code runs fine for hours and as soon as I update the GUI I run into problems within a couple of minutes.

What I have tried:

What I have already tried:

- make all variables of the property objects volatile

- delegates via
C#
Action b = delegate { windowmodel.Frame_Current = FrameX };
Application.Current.Dispatcher.Invoke(b, DispatcherPriority.Normal);

- locks:
C#
lock (windowmodel.AudioLevelArray)



Any idea how to get the GUI related properties thread-safe?
Posted
Updated 29-Jan-18 4:18am
Comments
George Swan 26-Jan-18 11:54am    
Are you reporting the progress of your asynchronous method or are you only updating the UI when your method completes?
VR Karthikeyan 29-Jan-18 2:12am    
What is the actual problem you are facing while updating the GUI, is there any exception or error message?
Richard Deeming 30-Jan-18 11:56am    

1 solution

run into problems within a couple of minutes


What Problems? From the rest of the question it sounds like you suspect multithreading to be the problem. But you didn't show any code related to that, you mentioned some default "solution patterns" for such Problems, but how can we tell if you applied them correctly?

What I can say (working with WPF and MVVM for years) that multithreading is no problem if you dispatch changes from other threads correctly.

So if your (async) method doesn't report Progress - just report back the result on the correct (GUI)-thread (by dispatching) - don't use any shared variables to make your life easy.

But I suspect you may have some other problem... because if you did something wrong with dispatching it normally won't take minutes to generate an error (or does your async method runs for that long?)

So I stop quesswork now - Show us the real error or exception - then we can try to find the reason..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900