关于线程得到结果,想在主窗体界面显示的问题 首先你在线程里面更新LISTBOX应该不行。你必须调用ListBox的Invoke方法才能更新线程外的ListBox控件。同步问题很容易解决,用临界区或信号量都可以。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面是我写的一个线程程序,共楼主参考。using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Threading;namespace WindowsApplication1{ //定义代理 public delegate void OccuredHandler(string msg); /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { //定义事件 public event OccuredHandler Occured; //定义线程 Thread myThread; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button2; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); //注册事件 this.Occured +=new OccuredHandler(Form1_Occured); myThread = new Thread(new ThreadStart(RunThread)); } /// <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.button1 = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(184, 32); this.button1.Name = "button1"; this.button1.TabIndex = 0; this.button1.Text = "运行"; this.button1.Click += new System.EventHandler(this.button1_Click); // // label1 // this.label1.Location = new System.Drawing.Point(32, 112); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(192, 23); this.label1.TabIndex = 1; this.label1.Text = "label1"; // // button2 // this.button2.Location = new System.Drawing.Point(184, 72); this.button2.Name = "button2"; this.button2.TabIndex = 2; this.button2.Text = "退出"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button2); this.Controls.Add(this.label1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } //线程方法 public void RunThread() { for(int i=0;i<1000000;i++) { this.label1.Text = i.ToString(); } //激发事件 Occured("终于结束了,大哥快冒烟了!"); } //线程结束通知函数 private void Form1_Occured(string msg) { MessageBox.Show(msg); } private void button1_Click(object sender, System.EventArgs e) { myThread.Start(); this.button1.Enabled = false; } private void button2_Click(object sender, System.EventArgs e) { //if(myThread.ThreadState!=ThreadState.Stopped||myThread.ThreadState!=ThreadState.Aborted) myThread.Abort(); Application.Exit(); } }} C# 中如何去除指定的字符 wpf listview 如何得到按钮所在行? 在调用 SubmitChanges 期间不能执行此操作 图片 RTF格式 SQL编写 byte[] 和 string 怎么连接? [WinForm]ComboBox问题,在线等,请帮个忙 log4net 配置文件的路径问题,请指教 .net 好书下载 在win8.1操作系统下如果开机自动打开特定程序 如何显示n个"."? 请问如何用C#编译word控件啊?请各位大哥赐教.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;namespace WindowsApplication1
{
//定义代理
public delegate void OccuredHandler(string msg);
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
//定义事件
public event OccuredHandler Occured;
//定义线程
Thread myThread;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
InitializeComponent();
//注册事件
this.Occured +=new OccuredHandler(Form1_Occured);
myThread = new Thread(new ThreadStart(RunThread));
} /// <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.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(184, 32);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "运行";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 112);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(192, 23);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(184, 72);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "退出";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
//线程方法
public void RunThread()
{
for(int i=0;i<1000000;i++)
{
this.label1.Text = i.ToString();
}
//激发事件
Occured("终于结束了,大哥快冒烟了!");
}
//线程结束通知函数
private void Form1_Occured(string msg)
{
MessageBox.Show(msg);
} private void button1_Click(object sender, System.EventArgs e)
{
myThread.Start();
this.button1.Enabled = false;
} private void button2_Click(object sender, System.EventArgs e)
{
//if(myThread.ThreadState!=ThreadState.Stopped||myThread.ThreadState!=ThreadState.Aborted)
myThread.Abort();
Application.Exit();
}
}
}