📄 mainform.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
//Note You must be careful not to manipulate any user-interface objects
//in your System.ComponentModel.BackgroundWorker.DoWork event handler.
//Instead, communicate to the user interface through the
//System.ComponentModel.BackgroundWorker.ProgressChanged and
//System.ComponentModel.BackgroundWorker.RunWorkerCompleted events.
namespace BackgroudWokerUI
{
public partial class MainForm : Form
{
//BindingList is useful list for UI
private IList<string> leftList = new BindingList<string>();
private IList<string> rightList = new BindingList<string>();
private BackgroundWorker worker = null;
public MainForm()
{
InitializeComponent();
//Databinding here
listBox1.DataSource = leftList;
listBox2.DataSource = rightList;
}
private void addButton_Click(object sender, EventArgs e)
{
if (textBox.Text.Length != 0)
{
leftList.Add(textBox.Text);
textBox.Text = "";
textBox.Focus();
}
}
private void moveButton_Click(object sender, EventArgs e)
{
//Show the progress bar
ProgressForm progressForm = new ProgressForm();
progressForm.Show();
// Prepare the background worker for asynchronous prime number calculation
worker= new BackgroundWorker();
// Specify that the background worker provides progress notifications
worker.WorkerReportsProgress = true;
// Specify that the background worker supports cancellation
worker.WorkerSupportsCancellation = true;
// The DoWork event handler is the main work function of the background thread
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
// Specify the function to use to handle progress
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.ProgressChanged += new ProgressChangedEventHandler(progressForm.OnProgressChanged);
// Specify the function to run when the background worker finishes
// There are three conditions possible that should be handled in this function:
// 1. The work completed successfully
// 2. The work aborted with errors
// 3. The user cancelled the process
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerCompleted+=new RunWorkerCompletedEventHandler(progressForm.OnProcessCompleted);
//If your background operation requires a parameter,
//call System.ComponentModel.BackgroundWorker.RunWorkerAsync
//with your parameter. Inside the System.ComponentModel.BackgroundWorker.DoWork
//event handler, you can extract the parameter from the
//System.ComponentModel.DoWorkEventArgs.Argument property.
worker.RunWorkerAsync(leftList);
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
MoveList((BackgroundWorker)sender,e);
}
private void MoveList(BackgroundWorker worker,DoWorkEventArgs e)
{
IList<string> list = e.Argument as IList<string>;
for (int i = 0; i < list.Count; i++)
{
// Check for cancellation
if (worker.CancellationPending)
{
e.Cancel = true;
break;
}
else
{
// This will be handled in the correct thread thanks to the
// internals of BackgroundWroker and AsyncOperation
worker.ReportProgress((i + 1) * (100 / list.Count), list[i]);
// Simulate some time consuming proccess.
Thread.Sleep(500);
}
}
}
private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//Add string to the right listBox
rightList.Add(e.UserState as string);
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
label.Text = "Cancelled";
}
else if (e.Error != null)
{
label.Text = "Error";
}
else
{
label.Text = "Success!";
leftList.Clear();
}
}
private void cancelButton_Click(object sender, EventArgs e)
{
if (worker.IsBusy)
{
label.Text = "Cancelling...";
worker.CancelAsync();
}
}
private void moveBackButton_Click(object sender, EventArgs e)
{
foreach (string str in rightList)
{
leftList.Add(str);
}
rightList.Clear();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -