I make a following sample to attempt to dynamic update my UI while long lasting operation running, but the UI hang while it runs.
Here is my entity class:
public class ClsTest : INotifyPropertyChanged
{
private string _text;
public string Text
{
get { return _text; }
set
{
if (string.IsNullOrEmpty(value) && value == _text)
return;
_text = value;
NotifyPropertyChanged(() => Text);
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged<T>(Expression<Func<T>> property)
{
if (PropertyChanged == null)
return;
var memberExpression = property.Body as MemberExpression;
if (memberExpression == null)
return;
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(memberExpression.Member.Name));
}
}
And here is my binding code in my main form_load
ClsTest x = new ClsTest();
x.Text = "yes";
txtValue.DataBindings.Add("Text", x, "Text", false, DataSourceUpdateMode.Never);
When I run following code in a button click method, it hangs:
for (int i = 0; i < 20; i++)
{
x.Text = i.ToString();
System.Threading.Thread.Sleep(1000);
}
How can I resolve this problem?