这个类很特别

[ 9446 查看 / 1 回复 ]

AsyncOperation asyncOperation;
SendOrPostCallback progressReporter;
Thread workerThread;

public MainForm()
{
    InitializeComponent();

    asyncOperation = AsyncOperationManager.CreateOperation(null);
    progressReporter = new SendOrPostCallback(ReportProgress);
    workerThread = new Thread(new ThreadStart(WorkOnWorkerThread));
}

private void MainForm_Load(object sender, EventArgs e)
{
    Thread.CurrentThread.Name = "Main Thread";
    workerThread.Name = "Worker Thread";
    workerThread.IsBackground = true;
    workerThread.Start();
}

void ReportProgress(object obj)
{
    this.Text = obj.ToString() + "% - " + Thread.CurrentThread.Name;
}

void WorkOnWorkerThread()
{
    int i = 0;
    while (i++ < 100)
    {
        asyncOperation.Post(progressReporter, i);
        Thread.Sleep(50);
    }
}
TOP

不懂
TOP