async private void BreadCompletedHandler1(Oven oven, Bread bread) { Action updateOutputText = () => { OvenClientOutput.Text += "Event Handler 1: Invoked\n"; OvenClientOutput.Text += "Event Handler 1: Oven volume is: " + oven.Volume.ToString() + "\n"; OvenClientOutput.Text += "Event Handler 1: Bread flavor is: " + bread.Flavor + "\n"; }; if (OvenClientOutput.Dispatcher.HasThreadAccess) { // If the current thread is the UI thread then execute the lambda. updateOutputText(); } else { // If the current thread is not the UI thread use the dispatcher to execute the lambda on the UI thread. await OvenClientOutput.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(updateOutputText)); } }
public void BakeBread(String flavor) { TimeSpan preheatTime; // Determine how long to wait for the oven to get to temperature switch (_temperature) { case OvenTemperature.Low: // Set the preheat time to 100ms preheatTime = new TimeSpan(1000); break; case OvenTemperature.Medium: // Set the preheat time to 200ms preheatTime = new TimeSpan(2000); break; case OvenTemperature.High: // Set the preheat time to 300ms preheatTime = new TimeSpan(3000); break; default: throw new ArgumentException("Temperature is out of range. The temperature should be low, medium, or high."); } // Set up a threadpool work item to bake the bread and notify listeners after the oven has preheated ThreadPoolTimer.CreateTimer(new TimerElapsedHandler( (ThreadPoolTimer timer) => { Bread bread = new Bread(flavor); if (BreadBaked != null) { BreadBaked(this, bread); } }), preheatTime); }
async private void BreadCompletedHandler3(Oven oven, Bread bread) { // Event handler 3 was removed and will not be invoked Action updateOutputText = () => { OvenClientOutput.Text += "Event Handler 3: Invoked\n"; }; if (OvenClientOutput.Dispatcher.HasThreadAccess) { // If the current thread is the UI thread then execute the lambda. updateOutputText(); } else { // If the current thread is not the UI thread use the dispatcher to execute the lambda on the UI thread. await OvenClientOutput.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, new Windows.UI.Core.DispatchedHandler(updateOutputText)); } }