As explained in the previous section, the notification is received by a background thread and not by the GUI thread on which the application is running. Although the refresh method cannot be run directly it can be invoked using a delegate.
In the MainWindow.xaml.cs code file, define a delegate type for refreshing the display that takes no parameters called RefreshDelegate and declare a member of this type.
public partial class MainWindow: Window
{
private JoobContext context;
private Bank bank;
delegate void RefreshDelegate();
RefreshDelegate refreshDelegate;
In the form constructor, point the refreshDelegate member to the refresh method.
public MainWindow()
{
InitializeComponent();
context = new JoobContext();
bank = context.FirstInstance<Bank>();
context.RegisterClassNotificationHandler(typeof(Customer),
NotificationEventConstants.SystemCreate,
customerCreatedNoteHandler);
refreshDelegate = new RefreshDelegate(refresh);
In the customerCreatedNoteHandler notification event handler method, use the Invoke method to marshal the refresh method to the GUI thread.
private void customerCreatedNoteHandler(object sender, NotificationEventArgs e)
{
MessageBox.Show("Customer added");
Dispatcher.Invoke(refreshDelegate);
}
Run the BankingApp application and then enter data for a new customer. Click the Add button to add the customer. After the message box is closed, the list of customers is refreshed automatically.