void _Start() { if (status == AdvertisementWatcherStatus.Started) { throw new InvalidOperationException("Watcher is already running"); } if (status == AdvertisementWatcherStatus.Stopping) { throw new InvalidOperationException("Watcher is still stopping"); } // following Windows API by setting Started status immediately status = AdvertisementWatcherStatus.Started; stopError = AdvertisementWatcherError.None; StartAsync().ContinueWith(t => { if (t.Status != TaskStatus.RanToCompletion) { status = AdvertisementWatcherStatus.Aborted; // TODO: more specific errors based on exception stopError = AdvertisementWatcherError.Unknown; Stop(); } }); }
void _Stop() { if (status != AdvertisementWatcherStatus.Started) { throw new InvalidOperationException("Watcher is not running"); } status = AdvertisementWatcherStatus.Stopping; StopAsync().ContinueWith(t => { if (t.IsCompleted) { status = AdvertisementWatcherStatus.Stopped; Stopped?.Invoke(this, new AdvertisementWatcherStoppedEventArgs(stopError)); } }); }