2

Trying to get clear about flaw in this code:

Scenario 1: This scenario uses data binding and causes the very well known cross-thread exception in the NotifyPropertyChanged() method in the PriceSimulator class.

Scenario 2: This scenario solves the problem by subscribing to the PropertyChanged event of PriceSimulator, eliminates the cross-thread issue but has to avoid data binding altogether.

Assuming Scenario 1 was the intended scenario and assuming one has no knowledge of the inner workings of PriceSimulator and just wanted to bind to the Price property, what is the core issue here?

Form1.cs:

public partial class Form1 : Form
{
    PriceSimulator simul;
    Action labelAction;

    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        labelAction = new Action(SetLabel);

        simul = new PriceSimulator(5, 1000);

        //Scenario 1:
        //Use data binding and get Cross-Thread exception
        //label1.DataBindings.Add("Text", simul, "Price");

        //Scenario 2:
        //This works fine
        //Subscribe to PropertyChanged event
        simul.PropertyChanged += task_PropertyChanged;

        simul.Start();
    }

    //Scenario 2:
    void task_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if (label1.InvokeRequired)
            Invoke(labelAction);
        else SetLabel();
    }
    private void SetLabel()
    {
        label1.Text = simul.Price.ToString("C2"); 
    }
}

PriceSimulator.cs:

public class PriceSimulator : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged; 

    private int max, delay, priceValue;
    private Timer timer;

    public PriceSimulator(int max, int delay)
    {
        this.max = max;
        this.delay = delay;
    }
    public void Start()
    {
        timer = new Timer(CallbackProc, null, delay, delay);
    }
    private void CallbackProc(object obj)
    {
        if (++Price >= max)
            timer.Dispose();
    }
    private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
    {
        try
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        catch (Exception ex)
        {
            timer.Dispose();
            System.Windows.Forms.MessageBox.Show(ex.Message);
        }
    }
    public int Price 
    {
        get
        {
            return priceValue;
        }
        set
        {
            if (priceValue != value)
            {
                priceValue = value;
                NotifyPropertyChanged();
            }
        }
    }
}

1 Answer 1

3

You have to have the current context in your PriceSimulator class:

private readonly SynchronizationContext _context = SynchronizationContext.Current;

Now that you have the context, you can use it to update the UI:

 _context.Post(delegate
                {
                    if (++Price >= max)
                       timer.Dispose();
                }, null);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.