private void stopScan_Click(object sender, RoutedEventArgs e) { backgroundService.stopService(); RackTextBox.Text = ""; BooksDataGrid.ItemsSource = null; uiBook = null; uiShelf = null; }
/*** private void updateBookInformation(string str) //book list should be array list { //ctrl.Content = str; //statusLabel.Content = str; //statusLabel.Dispatcher.Invoke(() => { statusLabel.Content = str; }); } * **/ /*** private void updateLocationInformation(string str) { //ctrl.Content = str; //statusLabel.Content = str; //statusLabel.Dispatcher.Invoke(() => { statusLabel.Content = str; }); } * **/ private void updateUiBookInformation(Dictionary<string, string> scanItemList) { uiBook = new List<BookForDisplay>(); //共享的数据,用于在表格中显示图书信息,可能需要加锁 uiShelf = null; //共享的数据,用于显示层架数据,可能需要加锁 int count = 1; lock (lockForUiBookAndUiShelf) //该部分一般被后台线程调用,更新了uiBook和uiShelf,需要加锁 { foreach (KeyValuePair<string, string> item in scanItemList) { try { //先清除数据库状态位置所留下的信息!! databaseStatusLabel.Dispatcher.Invoke(() => { databaseStatusLabel.Content = ""; }); if (item.Value.Equals("book")) { BookForDisplay oneBook = new BookForDisplay(); oneBook.id = count; oneBook.bookName = bookdatabase.getBookNameByDecodedRFID(item.Key); oneBook.scanCode = item.Key; uiBook.Add(oneBook); count = count + 1; } if (item.Value.Equals("shelf")) { String decodedShelfRFIDCode = item.Key; uiShelf = shelfdatabase.getShelfLocation(decodedShelfRFIDCode); String shelfLocationString = shelfdatabase.ShelfLocationInformationToString(uiShelf); RackTextBox.Dispatcher.Invoke(() => { RackTextBox.Text = shelfLocationString; }); } } catch (DataBaseQueryException) { databaseStatusLabel.Dispatcher.Invoke(() => { databaseStatusLabel.Content = "查不到该条目信息!"; }); continue;//某一个条码可能在数据库中找不到,但不能因此中断程序 } catch(DataBaseConnectException) { databaseStatusLabel.Dispatcher.Invoke(() => { databaseStatusLabel.Content = "数据库连接失败!"; }); } catch(Exception) { continue; } } //当用户点击停止扫描时,状态位被置false,但后台线程可能还要运行,在这期间还会刷新主界面 //因此当检查到该位为false立即停止更新ui,该程序片段必须放在刷新BookUI以前 if (backgroundService.serviceStatus == false) { BooksDataGrid.Dispatcher.Invoke(() => { RackTextBox.Text = ""; BooksDataGrid.ItemsSource = null; }); return; } BooksDataGrid.Dispatcher.Invoke(() => { //BooksDataGrid.ItemsSource = null; BooksDataGrid.ItemsSource = uiBook; //实现自动滚到最后一行 BooksDataGrid.ScrollIntoView(BooksDataGrid.Items[BooksDataGrid.Items.Count - 1]); }); } }