日期:2014-05-20  浏览次数:21111 次

线程间操作无效:从不是创建控件“XX”的线程访问它
using   System;
using   System.Drawing;
using   System.Collections;
using   System.ComponentModel;
using   System.Windows.Forms;
using   System.Data;
using   System.Threading;

namespace   PrimeTest
{
///   <summary>
///   Form1   的摘要说明。
///   </summary>
public   class   Form1   :   System.Windows.Forms.Form
{
private   Thread   primeThread;
private   System.Windows.Forms.ListBox   lstPrime;
private   System.Windows.Forms.Button   btnStart;
private   System.Windows.Forms.Button   btnPause;
private   System.Windows.Forms.Button   btnResume;
private   System.Windows.Forms.Button   btnStop;

                 

///   <summary>
///   必需的设计器变量。
///   </summary>
private   System.ComponentModel.Container   components   =   null;

public   Form1()
{
//
//   Windows   窗体设计器支持所必需的
//
InitializeComponent();

//
//   TODO:   在   InitializeComponent   调用后添加任何构造函数代码
//
}

///   <summary>
///   清理所有正在使用的资源。
///   </summary>
protected   override   void   Dispose(   bool   disposing   )
{
if(   disposing   )
{
if   (components   !=   null)  
{
components.Dispose();
}
}
base.Dispose(   disposing   );
}

#region   Windows   窗体设计器生成的代码
///   <summary>
///   设计器支持所需的方法   -   不要使用代码编辑器修改
///   此方法的内容。
///   </summary>
private   void   InitializeComponent()
{
this.lstPrime   =   new   System.Windows.Forms.ListBox();
this.btnStart   =   new   System.Windows.Forms.Button();
this.btnPause   =   new   System.Windows.Forms.Button();
this.btnResume   =   new   System.Windows.Forms.Button();
this.btnStop   =   new   System.Windows.Forms.Button();
this.SuspendLayout();
//  
//   lstPrime
//  
this.lstPrime.ItemHeight   =   12;
this.lstPrime.Location   =   new   System.Drawing.Point(8,   16);
this.lstPrime.Name   =   "lstPrime ";
this.lstPrime.Size   =   new   System.Drawing.Size(336,   160);
this.lstPrime.TabIndex   =   0;
//  
//   btnStart
//  
this.btnStart.Location   =   new   System.Drawing.Point(8,   192);
this.btnStart.Name   =   "btnStart ";
this.btnStart.TabIndex   =   1;
this.btnStart.Text   =   "开始(&S) ";
this.btnStart.Click   +=   new   System.EventHandler(this.btnStart_Click);
//  
//   btnPause
//  
this.btnPause.Location   =   new   System.Drawing.Point(96,   192);
this.btnPause.Name   =   "btnPause ";
this.btnPause.TabIndex   =   2;
this.btnPause.Text   =   "暂停(&P) ";
this.btnPause.Click   +=   new   System.EventHandler(this.btnPause_Click);
//