Winforms binds data to business objects in a multi-threaded script without InvokeRequired?

For example, I have a business object Person:

class Person : INotifyPropertyChanged
{
    string Name { get; set; }
    DateTime DateOfBirth { get; set; }
}
// ^ abbreviated for better legibility; implementation would be trivial

And I have some Winforms UI that manages data bound to an object of this class:

Person somePerson = ...;
nameTextBox.DataBindings.Add("Text", somePerson, "Name");
dobDatePicker.DataBindings.Add("Value", somePerson, "DateOfBirth");

Now I am making changes to somePersonand thanks to the implementation, INotifyPropertyChangedthese changes are reflected in the user interface. So far so good.

Now to my problem: If I make changes to somePersonthe workflow (i.e. not the user interface thread), for example. because I am loading data from the database as a background operation, this can throw exceptions because data binding is trying to update controls, which is only allowed in the user interface thread.

, InvokeRequired , , - — .

-, , . Winforms ?

+1
1

, .

- , , -, , .

:

  • : - , - . . - , , ( ), ( ).
  • : async . "" , ; .
  • :. async , " " . GUI -, , , .

System.Threading.Tasks.Task . .NET 4.0, .NET 3.5.

(1) (2) - , Task - . (3), Task TaskScheduler.FromCurrentSynchronizationContext . ( FromCurrentSynchronizationContext GUI, (1), .)

+3

All Articles