WPF, MVVM: lag between the moment OnPropertyChanged is fired and UI refresh
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have implemented a plain vanilla MVVM apps. I have a datagrid bound to an ObservableCollection. When the database is refreshed, the collection is updated, the OnPropertyChanged is correctly fired but there is a lag before the UI is properly refreshed. I can tell that because when I debug, I can see that there is a few second between the moment the OnPropertyChanged is fired and the get accessor of the ObservableCollection is called. The lag does not seems to be correlated to the number of rows (ie 5 or 50 rows, it seems that there is the same lag).
Where can I start to find out where does this lag comes from ?
Code:
It is plain vanille MVVM hence:
private ObservableCollection<MyVar> _MyVars;
/// <summary>
/// List of pnl
/// </summary>
public ObservableCollection<MyVar> MyVars
get
return _MyVars;
set
_MyVars = value;
OnPropertyChanged("MyVars");
The ObservableCollection MyVars is set by a call to a database. Basically what is happening is a lag between the call to OnPropertyChanged("MyVars"); and the call to return _MyVars;. MyVars is the item source for a datagrid.
Both in release and debug, the datagrid takes a few seconds to refresh and I found out that the lag is coming between the two calls I mentionned above. I can't put more code than that, I was just wondering If somebody had the same situation and which path did he/she took to debug and solve the problem.
Thanks,
Pierre
wpf mvvm inotifypropertychanged
add a comment |
I have implemented a plain vanilla MVVM apps. I have a datagrid bound to an ObservableCollection. When the database is refreshed, the collection is updated, the OnPropertyChanged is correctly fired but there is a lag before the UI is properly refreshed. I can tell that because when I debug, I can see that there is a few second between the moment the OnPropertyChanged is fired and the get accessor of the ObservableCollection is called. The lag does not seems to be correlated to the number of rows (ie 5 or 50 rows, it seems that there is the same lag).
Where can I start to find out where does this lag comes from ?
Code:
It is plain vanille MVVM hence:
private ObservableCollection<MyVar> _MyVars;
/// <summary>
/// List of pnl
/// </summary>
public ObservableCollection<MyVar> MyVars
get
return _MyVars;
set
_MyVars = value;
OnPropertyChanged("MyVars");
The ObservableCollection MyVars is set by a call to a database. Basically what is happening is a lag between the call to OnPropertyChanged("MyVars"); and the call to return _MyVars;. MyVars is the item source for a datagrid.
Both in release and debug, the datagrid takes a few seconds to refresh and I found out that the lag is coming between the two calls I mentionned above. I can't put more code than that, I was just wondering If somebody had the same situation and which path did he/she took to debug and solve the problem.
Thanks,
Pierre
wpf mvvm inotifypropertychanged
Can you post some of your code for us to look at, otherwise assumptions will be made which may lead you down the wrong path.
– Coops
Nov 16 '18 at 12:00
During debugging there's a lot going on so it might not be representative of what happens in an actual release build of the application. Is there also a lag when just running it without debugger?
– stijn
Nov 16 '18 at 12:11
add a comment |
I have implemented a plain vanilla MVVM apps. I have a datagrid bound to an ObservableCollection. When the database is refreshed, the collection is updated, the OnPropertyChanged is correctly fired but there is a lag before the UI is properly refreshed. I can tell that because when I debug, I can see that there is a few second between the moment the OnPropertyChanged is fired and the get accessor of the ObservableCollection is called. The lag does not seems to be correlated to the number of rows (ie 5 or 50 rows, it seems that there is the same lag).
Where can I start to find out where does this lag comes from ?
Code:
It is plain vanille MVVM hence:
private ObservableCollection<MyVar> _MyVars;
/// <summary>
/// List of pnl
/// </summary>
public ObservableCollection<MyVar> MyVars
get
return _MyVars;
set
_MyVars = value;
OnPropertyChanged("MyVars");
The ObservableCollection MyVars is set by a call to a database. Basically what is happening is a lag between the call to OnPropertyChanged("MyVars"); and the call to return _MyVars;. MyVars is the item source for a datagrid.
Both in release and debug, the datagrid takes a few seconds to refresh and I found out that the lag is coming between the two calls I mentionned above. I can't put more code than that, I was just wondering If somebody had the same situation and which path did he/she took to debug and solve the problem.
Thanks,
Pierre
wpf mvvm inotifypropertychanged
I have implemented a plain vanilla MVVM apps. I have a datagrid bound to an ObservableCollection. When the database is refreshed, the collection is updated, the OnPropertyChanged is correctly fired but there is a lag before the UI is properly refreshed. I can tell that because when I debug, I can see that there is a few second between the moment the OnPropertyChanged is fired and the get accessor of the ObservableCollection is called. The lag does not seems to be correlated to the number of rows (ie 5 or 50 rows, it seems that there is the same lag).
Where can I start to find out where does this lag comes from ?
Code:
It is plain vanille MVVM hence:
private ObservableCollection<MyVar> _MyVars;
/// <summary>
/// List of pnl
/// </summary>
public ObservableCollection<MyVar> MyVars
get
return _MyVars;
set
_MyVars = value;
OnPropertyChanged("MyVars");
The ObservableCollection MyVars is set by a call to a database. Basically what is happening is a lag between the call to OnPropertyChanged("MyVars"); and the call to return _MyVars;. MyVars is the item source for a datagrid.
Both in release and debug, the datagrid takes a few seconds to refresh and I found out that the lag is coming between the two calls I mentionned above. I can't put more code than that, I was just wondering If somebody had the same situation and which path did he/she took to debug and solve the problem.
Thanks,
Pierre
wpf mvvm inotifypropertychanged
wpf mvvm inotifypropertychanged
edited Nov 16 '18 at 14:59
PDM
asked Nov 16 '18 at 11:52
PDMPDM
177
177
Can you post some of your code for us to look at, otherwise assumptions will be made which may lead you down the wrong path.
– Coops
Nov 16 '18 at 12:00
During debugging there's a lot going on so it might not be representative of what happens in an actual release build of the application. Is there also a lag when just running it without debugger?
– stijn
Nov 16 '18 at 12:11
add a comment |
Can you post some of your code for us to look at, otherwise assumptions will be made which may lead you down the wrong path.
– Coops
Nov 16 '18 at 12:00
During debugging there's a lot going on so it might not be representative of what happens in an actual release build of the application. Is there also a lag when just running it without debugger?
– stijn
Nov 16 '18 at 12:11
Can you post some of your code for us to look at, otherwise assumptions will be made which may lead you down the wrong path.
– Coops
Nov 16 '18 at 12:00
Can you post some of your code for us to look at, otherwise assumptions will be made which may lead you down the wrong path.
– Coops
Nov 16 '18 at 12:00
During debugging there's a lot going on so it might not be representative of what happens in an actual release build of the application. Is there also a lag when just running it without debugger?
– stijn
Nov 16 '18 at 12:11
During debugging there's a lot going on so it might not be representative of what happens in an actual release build of the application. Is there also a lag when just running it without debugger?
– stijn
Nov 16 '18 at 12:11
add a comment |
1 Answer
1
active
oldest
votes
Why are you re-assinging an ObservableCollection property? Just create it once and add / remove items as required.
private ObservableCollection<MyVar> _myVarsCollection = new ObservableCollection<MyVar>();
public IEnumerable<MyVar> MyVars => _myVarsCollection;
public void AddMyVar(MyVar myVar)
_myVarsCollection.Add(myVar);
public void AddMyVars(IEnumerable<MyVar> myVars)
foreach(var myvar in myVars)
AddMyVar(myvar);
public void RemoveMyVar(MyVar myVar)
_myVarsCollection.Remove(myVar);
Even though _myVarsCollection is not public, any control bound to MyVars will detect that it implements INotifyCollectionChanged and will react accordingly when items are added / removed.
I was thinking that it is equivalent to add/remove items or to reassigned the whole collection altogether. Thanks I will try that.
– PDM
Nov 16 '18 at 15:06
Good thanks! This implemetation worked. Still I don't get the difference issue between my solution and yours. Any insight on some docs will be really appreciated.
– PDM
Nov 16 '18 at 16:17
1
If you refresh the list property, then the bound control has to completely re-create and re-draw all of the controls for each item - this is probably a good chuck of your lag. If you just add new items to the ObservableCollection then there's much less work to do.
– Peregrine
Nov 16 '18 at 19:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337356%2fwpf-mvvm-lag-between-the-moment-onpropertychanged-is-fired-and-ui-refresh%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why are you re-assinging an ObservableCollection property? Just create it once and add / remove items as required.
private ObservableCollection<MyVar> _myVarsCollection = new ObservableCollection<MyVar>();
public IEnumerable<MyVar> MyVars => _myVarsCollection;
public void AddMyVar(MyVar myVar)
_myVarsCollection.Add(myVar);
public void AddMyVars(IEnumerable<MyVar> myVars)
foreach(var myvar in myVars)
AddMyVar(myvar);
public void RemoveMyVar(MyVar myVar)
_myVarsCollection.Remove(myVar);
Even though _myVarsCollection is not public, any control bound to MyVars will detect that it implements INotifyCollectionChanged and will react accordingly when items are added / removed.
I was thinking that it is equivalent to add/remove items or to reassigned the whole collection altogether. Thanks I will try that.
– PDM
Nov 16 '18 at 15:06
Good thanks! This implemetation worked. Still I don't get the difference issue between my solution and yours. Any insight on some docs will be really appreciated.
– PDM
Nov 16 '18 at 16:17
1
If you refresh the list property, then the bound control has to completely re-create and re-draw all of the controls for each item - this is probably a good chuck of your lag. If you just add new items to the ObservableCollection then there's much less work to do.
– Peregrine
Nov 16 '18 at 19:55
add a comment |
Why are you re-assinging an ObservableCollection property? Just create it once and add / remove items as required.
private ObservableCollection<MyVar> _myVarsCollection = new ObservableCollection<MyVar>();
public IEnumerable<MyVar> MyVars => _myVarsCollection;
public void AddMyVar(MyVar myVar)
_myVarsCollection.Add(myVar);
public void AddMyVars(IEnumerable<MyVar> myVars)
foreach(var myvar in myVars)
AddMyVar(myvar);
public void RemoveMyVar(MyVar myVar)
_myVarsCollection.Remove(myVar);
Even though _myVarsCollection is not public, any control bound to MyVars will detect that it implements INotifyCollectionChanged and will react accordingly when items are added / removed.
I was thinking that it is equivalent to add/remove items or to reassigned the whole collection altogether. Thanks I will try that.
– PDM
Nov 16 '18 at 15:06
Good thanks! This implemetation worked. Still I don't get the difference issue between my solution and yours. Any insight on some docs will be really appreciated.
– PDM
Nov 16 '18 at 16:17
1
If you refresh the list property, then the bound control has to completely re-create and re-draw all of the controls for each item - this is probably a good chuck of your lag. If you just add new items to the ObservableCollection then there's much less work to do.
– Peregrine
Nov 16 '18 at 19:55
add a comment |
Why are you re-assinging an ObservableCollection property? Just create it once and add / remove items as required.
private ObservableCollection<MyVar> _myVarsCollection = new ObservableCollection<MyVar>();
public IEnumerable<MyVar> MyVars => _myVarsCollection;
public void AddMyVar(MyVar myVar)
_myVarsCollection.Add(myVar);
public void AddMyVars(IEnumerable<MyVar> myVars)
foreach(var myvar in myVars)
AddMyVar(myvar);
public void RemoveMyVar(MyVar myVar)
_myVarsCollection.Remove(myVar);
Even though _myVarsCollection is not public, any control bound to MyVars will detect that it implements INotifyCollectionChanged and will react accordingly when items are added / removed.
Why are you re-assinging an ObservableCollection property? Just create it once and add / remove items as required.
private ObservableCollection<MyVar> _myVarsCollection = new ObservableCollection<MyVar>();
public IEnumerable<MyVar> MyVars => _myVarsCollection;
public void AddMyVar(MyVar myVar)
_myVarsCollection.Add(myVar);
public void AddMyVars(IEnumerable<MyVar> myVars)
foreach(var myvar in myVars)
AddMyVar(myvar);
public void RemoveMyVar(MyVar myVar)
_myVarsCollection.Remove(myVar);
Even though _myVarsCollection is not public, any control bound to MyVars will detect that it implements INotifyCollectionChanged and will react accordingly when items are added / removed.
answered Nov 16 '18 at 14:57
PeregrinePeregrine
2,41021230
2,41021230
I was thinking that it is equivalent to add/remove items or to reassigned the whole collection altogether. Thanks I will try that.
– PDM
Nov 16 '18 at 15:06
Good thanks! This implemetation worked. Still I don't get the difference issue between my solution and yours. Any insight on some docs will be really appreciated.
– PDM
Nov 16 '18 at 16:17
1
If you refresh the list property, then the bound control has to completely re-create and re-draw all of the controls for each item - this is probably a good chuck of your lag. If you just add new items to the ObservableCollection then there's much less work to do.
– Peregrine
Nov 16 '18 at 19:55
add a comment |
I was thinking that it is equivalent to add/remove items or to reassigned the whole collection altogether. Thanks I will try that.
– PDM
Nov 16 '18 at 15:06
Good thanks! This implemetation worked. Still I don't get the difference issue between my solution and yours. Any insight on some docs will be really appreciated.
– PDM
Nov 16 '18 at 16:17
1
If you refresh the list property, then the bound control has to completely re-create and re-draw all of the controls for each item - this is probably a good chuck of your lag. If you just add new items to the ObservableCollection then there's much less work to do.
– Peregrine
Nov 16 '18 at 19:55
I was thinking that it is equivalent to add/remove items or to reassigned the whole collection altogether. Thanks I will try that.
– PDM
Nov 16 '18 at 15:06
I was thinking that it is equivalent to add/remove items or to reassigned the whole collection altogether. Thanks I will try that.
– PDM
Nov 16 '18 at 15:06
Good thanks! This implemetation worked. Still I don't get the difference issue between my solution and yours. Any insight on some docs will be really appreciated.
– PDM
Nov 16 '18 at 16:17
Good thanks! This implemetation worked. Still I don't get the difference issue between my solution and yours. Any insight on some docs will be really appreciated.
– PDM
Nov 16 '18 at 16:17
1
1
If you refresh the list property, then the bound control has to completely re-create and re-draw all of the controls for each item - this is probably a good chuck of your lag. If you just add new items to the ObservableCollection then there's much less work to do.
– Peregrine
Nov 16 '18 at 19:55
If you refresh the list property, then the bound control has to completely re-create and re-draw all of the controls for each item - this is probably a good chuck of your lag. If you just add new items to the ObservableCollection then there's much less work to do.
– Peregrine
Nov 16 '18 at 19:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337356%2fwpf-mvvm-lag-between-the-moment-onpropertychanged-is-fired-and-ui-refresh%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you post some of your code for us to look at, otherwise assumptions will be made which may lead you down the wrong path.
– Coops
Nov 16 '18 at 12:00
During debugging there's a lot going on so it might not be representative of what happens in an actual release build of the application. Is there also a lag when just running it without debugger?
– stijn
Nov 16 '18 at 12:11