0

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?

Occulter
  • 1
  • 3
  • It appears you're running this in UI thread itself. Obviously UI will hang. You need some kind of worker thread. Use `Task.Run` to run time consuming task. – Sriram Sakthivel Sep 10 '14 at 12:31
  • How to restructure the code to meet the requirement? then , thank u. – Occulter Sep 11 '14 at 06:06
  • Please refer my recent answers [here](http://stackoverflow.com/questions/25745176/how-to-provide-a-feedback-to-ui-in-a-async-method) and [here](http://stackoverflow.com/questions/25769078/wpf-async-task-locking-ui). If that doesn't help you update your question with what is the operation you're doing in place of `Thread.Sleep(1000)` ? Then I'll try to help. – Sriram Sakthivel Sep 11 '14 at 09:27

0 Answers0