/// /////////////////////////////////////////////////////////////// // Implementation void request_page(int page_num) { if (page_num < 0) { throw new ArgumentOutOfRangeException(); } if (!_dataRows.TryAdd(page_num, null)) // Return if the key already exists. { return; } ItemsEventArgs args = new ItemsEventArgs( _pageSize * page_num, // startIndex _pageSize, // requestedCount SortDescriptions.FirstOrDefault()); Task <List <T> > items_task = ItemsRequest(this, args); items_task.ContinueWith(task => { _dataRows[page_num] = task.Result; // Dictionary<>はthread-safe ではない! Console.WriteLine("Updated: " + page_num); Action action = () => { this.Refresh(); }; Application.Current.Dispatcher.BeginInvoke(action); // UIスレッドに戻す }); }
Task <List <DataRow> > OnItemsRequest(object sender, ItemsEventArgs arg2) { int startIndex = arg2.StartIndex; int count = arg2.RequestedCount; SortDescription sort = arg2.SortDescription; var task = Task <List <DataRow> > .Run(() => { List <DataRow> items = new List <DataRow>(); for (int i = startIndex; i < startIndex + count; i++) { DataRow row = dataTable.NewRow(); row[0] = i.ToString(); row[1] = rnd.Next(1000).ToString(); row[2] = rnd.Next(1000).ToString(); items.Add(row); } // Simulating heavy loading time Thread.Sleep(2 * 1000); return(items); }); return(task); }