⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Java聊天室基于C/S模式的简单的聊天室系统
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace ThreadSample
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.ListBox listBox2;
		private System.Windows.Forms.ListBox listBox3;
		private System.Windows.Forms.Button button4;
		private long num=0;
		/// <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.button3 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button1 = new System.Windows.Forms.Button();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.listBox2 = new System.Windows.Forms.ListBox();
			this.listBox3 = new System.Windows.Forms.ListBox();
			this.button4 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(200, 8);
			this.button3.Name = "button3";
			this.button3.TabIndex = 2;
			this.button3.Text = "MeesageBox";
			this.button3.Visible = false;
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(112, 8);
			this.button2.Name = "button2";
			this.button2.TabIndex = 1;
			this.button2.Text = "Color";
			this.button2.Visible = false;
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(24, 8);
			this.button1.Name = "button1";
			this.button1.TabIndex = 0;
			this.button1.Text = "Compute";
			this.button1.Visible = false;
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// listBox1
			// 
			this.listBox1.ItemHeight = 12;
			this.listBox1.Location = new System.Drawing.Point(16, 48);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(136, 352);
			this.listBox1.TabIndex = 3;
			// 
			// listBox2
			// 
			this.listBox2.ItemHeight = 12;
			this.listBox2.Location = new System.Drawing.Point(160, 48);
			this.listBox2.Name = "listBox2";
			this.listBox2.Size = new System.Drawing.Size(136, 352);
			this.listBox2.TabIndex = 4;
			// 
			// listBox3
			// 
			this.listBox3.ItemHeight = 12;
			this.listBox3.Location = new System.Drawing.Point(304, 48);
			this.listBox3.Name = "listBox3";
			this.listBox3.Size = new System.Drawing.Size(136, 352);
			this.listBox3.TabIndex = 5;
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(312, 8);
			this.button4.Name = "button4";
			this.button4.TabIndex = 6;
			this.button4.Text = "Count";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(456, 405);
			this.Controls.Add(this.button4);
			this.Controls.Add(this.listBox3);
			this.Controls.Add(this.listBox2);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			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());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			//compute();
			ThreadStart ts=new ThreadStart(this.compute);
			Thread t=new Thread(ts);
			t.Start();
		}

		private double factor(int n)
		{
			if (n==1) 
				return 1;
			if(n==0) 
				return 1;
			return n*factor(n-1);
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.BackColor=Color.LightBlue;
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("some message");
		}
		private void compute()
		{
			for(int i=0;i<3;i++)
				for(int j=0;j<1000;j++)
					for (int k=0;k<1000;k++)
						factor(100);
			MessageBox.Show("compute finished.");
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			ThreadStart ts1=new ThreadStart(this.count1);
			Thread t1=new Thread(ts1);
			ThreadStart ts2=new ThreadStart(this.count2);
			Thread t2=new Thread(ts2);
			ThreadStart ts3=new ThreadStart(this.count3);
			Thread t3=new Thread(ts3);
			t1.Start();
			t2.Start();
			t3.Start();
		}
		private void count1()
		{
			while (num<99999)
			{
				//num++;
				listBox1.Items.Add(num++);
				Thread.Sleep(1);
			}
		}
		private void count2()
		{
			while(num<99999)
			{
				//num++;
				listBox2.Items.Add(num++);
				Thread.Sleep(1);
			}
		}
		private void count3()
		{
			while(num<99999)
			{
				//num++;
				listBox3.Items.Add(num++);
				Thread.Sleep(1);
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -