private void ConventionalReadButton_Click(object sender, EventArgs e) { _customerBindingListView = null; dataGridView1.DataSource = null; ElapsedTimeLabel.Text = "00:00:00"; ElapsedTimeLabel.Refresh(); var watch = new Stopwatch(); watch.Start(); var customers = FileReader.ConventionalRead(_fileNameToReadFrom); _customerBindingListView = new SortableBindingList <Customer>(customers); _customersBindingSource.DataSource = _customerBindingListView; dataGridView1.DataSource = _customersBindingSource; dataGridView1.Columns["CustomerIdentifier"].Visible = false; /* * SuspendLayout and ResumeLayout will not really help */ if (ExpandColumnsCheckBox.Checked) { dataGridView1.SuspendLayout(); dataGridView1.ExpandColumns(); dataGridView1.ResumeLayout(); } ElapsedTimeLabel.Text = watch.Elapsed.ToString("mm\\:ss\\.ff"); }
/// <summary> /// Depending on several factors the first time asking for /// the current date time may take around five seconds and /// cause the user interface to become unresponsive without /// running code in a Task. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void GetCurrentDateTimeButton_Click(object sender, EventArgs e) { ElapsedTimeLabel.Text = "00-00-00"; ElapsedTimeLabel.Refresh(); var watch = new Stopwatch(); watch.Start(); var operation = new DateTimeFromInternet(); var result = await operation.Formatted(); watch.Stop(); ElapsedTimeLabel.Text = watch.Elapsed.ToString("mm\\:ss\\.ff"); if (Convert.ToDateTime(result) == DateTime.MinValue) { MessageBox.Show("Failed to get current date time"); } else { MessageBox.Show(result); } }
/// <summary> /// Read file using StreamReader.ReadLineAsync, create a new customer in each /// iteration in a while statement. /// /// First three lines are to allow the developer to see the sources are cleared /// before running the code to populate the DataGridView. /// /// This method is slow from combining reading lines/parsing then adding a customer to a list. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void Example1Button_Click(object sender, EventArgs e) { //dataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.EnableResizing; var waitForm = new WaitForm() { TopLevel = true, TopMost = true }; waitForm.Show(this); waitForm.Top = (Top + (Height / 2)) - waitForm.Height / 2; waitForm.Left = (Left + (Width / 2)) - waitForm.Width / 2; ElapsedTimeLabel.Text = "00:00:00"; ElapsedTimeLabel.Refresh(); var watch = new Stopwatch(); watch.Start(); try { _customerBindingListView = null; dataGridView1.DataSource = null; await Task.Delay(500); var customers = await FileReader.ReadAllLinesAsync(_fileNameToReadFrom).ConfigureAwait(false); _customerBindingListView = new SortableBindingList <Customer>(customers.ToList()); dataGridView1.InvokeIfRequired(d => { _customersBindingSource.DataSource = _customerBindingListView; dataGridView1.DataSource = _customersBindingSource; dataGridView1.Columns["CustomerIdentifier"].Visible = false; dataGridView1.ExpandColumns(); }); } finally { waitForm.Invoke((MethodInvoker)(() => waitForm.Dispose())); } watch.Stop(); ElapsedTimeLabel.Invoke((MethodInvoker)(() => ElapsedTimeLabel.Text = watch.Elapsed.ToString("mm\\:ss\\.ff"))); }
private void Example4Button_Click(object sender, EventArgs e) { ElapsedTimeLabel.Text = "00:00:00"; ElapsedTimeLabel.Refresh(); var watch = new Stopwatch(); watch.Start(); _customerBindingListView = new SortableBindingList <Customer>(new List <Customer>()); _customersBindingSource.DataSource = _customerBindingListView; dataGridView1.Visible = false; PleaseWaitLabel.Refresh(); try { _customerBindingListView = new SortableBindingList <Customer>(new List <Customer>()); _customersBindingSource.DataSource = _customerBindingListView; dataGridView1.DataSource = _customersBindingSource; foreach (var line in FileReader.ParseFile(_fileNameToReadFrom)) { var lineParts = line.Result.Split(','); _customerBindingListView.Add( new Customer() { CustomerIdentifier = Convert.ToInt32(lineParts[0]), CompanyName = lineParts[1], ContactName = lineParts[2], ContactTitle = lineParts[3], City = lineParts[4], Country = lineParts[5] }); } } finally { dataGridView1.Visible = true; dataGridView1.ExpandColumns(); watch.Stop(); ElapsedTimeLabel.Invoke((MethodInvoker)(() => ElapsedTimeLabel.Text = watch.Elapsed.ToString("mm\\:ss\\.ff"))); } }
private async void Example2Button_Click(object sender, EventArgs e) { bindingNavigator1.BindingSource = null; ElapsedTimeLabel.Text = "00:00:00"; ElapsedTimeLabel.Refresh(); var watch = new Stopwatch(); watch.Start(); _customerBindingListView = new SortableBindingList <Customer>(new List <Customer>()); _customersBindingSource.DataSource = _customerBindingListView; dataGridView1.DataSource = _customersBindingSource; int index = 0; foreach (var line in FileReader.ParseFile(_fileNameToReadFrom)) { var lineParts = line.Result.Split(','); if (index % _delayIndex == 0) { await Task.Delay(1); } _customerBindingListView.Add( new Customer() { CustomerIdentifier = Convert.ToInt32(lineParts[0]), CompanyName = lineParts[1], ContactName = lineParts[2], ContactTitle = lineParts[3], City = lineParts[4], Country = lineParts[5] }); index += 10; } watch.Stop(); bindingNavigator1.BindingSource = _customersBindingSource; ElapsedTimeLabel.Invoke((MethodInvoker)(() => ElapsedTimeLabel.Text = watch.Elapsed.ToString("mm\\:ss\\.ff"))); }
private async void Example5AButton_Click(object sender, EventArgs e) { ElapsedTimeLabel.Text = "00:00:00"; ElapsedTimeLabel.Refresh(); var watch = new Stopwatch(); watch.Start(); var customers = new List <Customer>(); var results = await FileReader.GetContentAsync(_fileNameToReadFrom); var lines = results.Split('\n'); foreach (var line in lines) { var lineParts = line.Split(','); customers.Add(new Customer() { CustomerIdentifier = Convert.ToInt32(lineParts[0]), CompanyName = lineParts[1], ContactName = lineParts[2], ContactTitle = lineParts[3], City = lineParts[4], Country = lineParts[5] }); } _customerBindingListView = new SortableBindingList <Customer>(customers.ToList()); _customersBindingSource.DataSource = _customerBindingListView; dataGridView1.DataSource = _customersBindingSource; watch.Stop(); ElapsedTimeLabel.Invoke((MethodInvoker)(() => ElapsedTimeLabel.Text = watch.Elapsed.ToString("mm\\:ss\\.ff"))); dataGridView1.RowValidating -= DataGridView1_RowValidating; dataGridView1.RowValidating += DataGridView1_RowValidating; RemoveCurrentButton.Enabled = true; }
private async void Example5Button_Click(object sender, EventArgs e) { var timeSpanList = new List <TimeSpan>(); ElapsedTimeLabel.Text = "00:00:00"; ElapsedTimeLabel.Refresh(); var watch = new Stopwatch(); watch.Start(); var results = await FileReader.GetContentAsync(_fileNameToReadFrom); watch.Stop(); timeSpanList.Add(watch.Elapsed); //Console.WriteLine($"Time to read file {watch.Elapsed.ToString("mm\\:ss\\.ff")}"); watch.Reset(); watch.Start(); var lines = results.Split('\n'); //Console.WriteLine($"Time to read file {watch.Elapsed.ToString("mm\\:ss\\.ff")}"); timeSpanList.Add(watch.Elapsed); watch.Reset(); watch.Start(); int index = 0; var customers = new List <Customer>(); foreach (var line in lines) { var lineParts = line.Split(','); customers.Add(new Customer() { CustomerIdentifier = Convert.ToInt32(lineParts[0]), CompanyName = lineParts[1], ContactName = lineParts[2], ContactTitle = lineParts[3], City = lineParts[4], Country = lineParts[5] }); if (index % _delayIndex == 0) { await Task.Delay(1); } index += 10; } timeSpanList.Add(watch.Elapsed); watch.Reset(); watch.Start(); _customerBindingListView = new SortableBindingList <Customer>(customers.ToList()); _customersBindingSource.DataSource = _customerBindingListView; dataGridView1.DataSource = _customersBindingSource; var totalSpan = new TimeSpan(timeSpanList.Sum(r => r.Ticks)); ElapsedTimeLabel.Text = totalSpan.ToString("mm\\:ss\\.ff"); }