Well IMHO opinion the other SO answer is not really the way to go for windows forms, although maybe not incorrect.
Normally you would use ISynchronizeInvoke
for such a feature in WinForms. Every container control implements this interface.
You'll need to BeginInvoke()
method to marshall the call back to the proper thread.
Based on your previous question the code would become:
public class SomeObject : INotifyPropertyChanged
{
private readonly ISynchronizeInvoke invoker;
public SomeObject(ISynchronizeInvoke invoker)
{
this.invoker = invoker;
}
public decimal AlertLevel
{
get { return alertLevel; }
set
{
if (alertLevel == value) return;
alertLevel = value;
OnPropertyChanged("AlertLevel");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
this.invoker.BeginInvoke((Action)(() =>
PropertyChanged(this, new PropertyChangedEventArgs(propertyName))), null);
}
}
}
Where you pass the owning Form
class to the constructor of SomeObject
. The PropertyChanged will now raised on the UI thread of the owning form class.