//-----Execute at page load----- public MainPage() { this.InitializeComponent(); for (int i = 0; i <= 99; i++) { FilePreview.Items.Add((string)"Text to send line " + i.ToString()); } FilePreview.SelectedIndex = 0; FilePreview.ScrollIntoView(FilePreview.SelectedItem); }
//-----Task for Feeding Lines to Serial Port private async Task FeedTask() { FeedActive = true; string s = ""; int i = 0; int c = 0; await this.Dispatcher.RunAsync(DispatchPriority, () => { i = FilePreview.SelectedIndex; c = FilePreview.Items.Count; }); while (i < c) { //*****Update Controls in the UI thread***** await this.Dispatcher.RunAsync(DispatchPriority, () => { s = FilePreview.SelectedItem.ToString(); }); //This commented code was used to bypass the Datawriter for testing /*s = s += "\r\n"; * StringBuilder sb = new StringBuilder(); * sb.Insert(0, s); * Windows.Storage.Streams.IBuffer b; * byte[] ba1 = Encoding.UTF8.GetBytes(s); * b = ba1.AsBuffer(); * uint x = await Port.OutputStream.WriteAsync(b);*/ s = s += "\r\n"; PortDataWriter.WriteString(s); Task <UInt32> storeAsyncTask; storeAsyncTask = PortDataWriter.StoreAsync().AsTask(); //<----This is where program hangs uint x = await storeAsyncTask; if (x != 0) { i++; if (i < c) { //*****Update Controls in the UI thread***** await this.Dispatcher.RunAsync(DispatchPriority, () => { FilePreview.SelectedIndex = i; FilePreview.ScrollIntoView(FilePreview.SelectedItem); }); } else { //*****Update Controls in the UI thread***** await this.Dispatcher.RunAsync(DispatchPriority, () => { FilePreview.SelectedIndex = 0; FilePreview.ScrollIntoView(FilePreview.SelectedItem); }); EnableFeed = false; } } if (EnableFeed == false) { break; } } //*****Update Controls in the UI thread***** await this.Dispatcher.RunAsync(DispatchPriority, () => { SingleBtn.IsEnabled = true; FeedBtn.Icon = new SymbolIcon(Symbol.Play); }); FeedActive = false; }