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

📄 frmprime.cs

📁 北大青鸟内部资料
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace Example_1
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class frmPrime : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnStop;
		private System.Windows.Forms.Button btnResume;
		private System.Windows.Forms.Button btnPause;
		private System.Windows.Forms.Button btnStart;
		private System.Windows.Forms.ListBox lstPrime;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmPrime()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnStop = new System.Windows.Forms.Button();
			this.btnResume = new System.Windows.Forms.Button();
			this.btnPause = new System.Windows.Forms.Button();
			this.btnStart = new System.Windows.Forms.Button();
			this.lstPrime = new System.Windows.Forms.ListBox();
			this.SuspendLayout();
			// 
			// btnStop
			// 
			this.btnStop.Location = new System.Drawing.Point(328, 232);
			this.btnStop.Name = "btnStop";
			this.btnStop.Size = new System.Drawing.Size(90, 25);
			this.btnStop.TabIndex = 9;
			this.btnStop.Text = "停止(&O)";
			this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
			// 
			// btnResume
			// 
			this.btnResume.Location = new System.Drawing.Point(216, 232);
			this.btnResume.Name = "btnResume";
			this.btnResume.Size = new System.Drawing.Size(90, 25);
			this.btnResume.TabIndex = 8;
			this.btnResume.Text = "恢复(&R)";
			this.btnResume.Click += new System.EventHandler(this.btnResume_Click);
			// 
			// btnPause
			// 
			this.btnPause.Location = new System.Drawing.Point(112, 232);
			this.btnPause.Name = "btnPause";
			this.btnPause.Size = new System.Drawing.Size(90, 25);
			this.btnPause.TabIndex = 7;
			this.btnPause.Text = "暂停(&P)";
			this.btnPause.Click += new System.EventHandler(this.btnPause_Click);
			// 
			// btnStart
			// 
			this.btnStart.Location = new System.Drawing.Point(8, 232);
			this.btnStart.Name = "btnStart";
			this.btnStart.Size = new System.Drawing.Size(90, 25);
			this.btnStart.TabIndex = 6;
			this.btnStart.Text = "开始(&S)";
			this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
			// 
			// lstPrime
			// 
			this.lstPrime.ItemHeight = 15;
			this.lstPrime.Location = new System.Drawing.Point(8, 8);
			this.lstPrime.Name = "lstPrime";
			this.lstPrime.Size = new System.Drawing.Size(408, 214);
			this.lstPrime.TabIndex = 5;
			// 
			// frmPrime
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.ClientSize = new System.Drawing.Size(424, 269);
			this.Controls.Add(this.btnStop);
			this.Controls.Add(this.btnResume);
			this.Controls.Add(this.btnPause);
			this.Controls.Add(this.btnStart);
			this.Controls.Add(this.lstPrime);
			this.Font = new System.Drawing.Font("Times New Roman", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmPrime";
			this.Text = "质数线程";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.frmPrime_Closing);
			this.Load += new System.EventHandler(this.frmPrime_Load);
			this.ResumeLayout(false);

		}
		#endregion

		// Thread variable
		private Thread primeThread;

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmPrime());
		}

		private void btnStart_Click(object sender, System.EventArgs e)
		{
			// New thread
			primeThread = new Thread(new ThreadStart(Generate));

			// Assigning name to thread
			primeThread.Name = "质数";
			primeThread.Priority = ThreadPriority.BelowNormal;

			// Enable the Pause Button
			btnPause.Enabled = true;
			// Disable the Start button
			btnStart.Enabled = false;
			// Enable the Stop button
			btnStop.Enabled = true;
			// Initiating the thread
			primeThread.Start();
			primeThread.Priority = ThreadPriority.Lowest;
		}

		private void btnStop_Click(object sender, System.EventArgs e)
		{
			// Enable and Disable buttons
			btnStop.Enabled = false;
			btnStart.Enabled = true;
			btnPause.Enabled = false;
			// Terminate thread execution
			primeThread.Abort();
		}

		private void btnPause_Click(object sender, System.EventArgs e)
		{
			try
			{
				// If current state of thread is running, then pause the Thread
				if (primeThread.ThreadState == ThreadState.Running)
				{
					// Pause the Thread
					primeThread.Suspend();
					// Disable the Pause button
					btnPause.Enabled = false;
					// Enable the resume button
					btnResume.Enabled = true;
					// Disable the Stop button
					btnStop.Enabled = false;
				}
			}
			catch(ThreadStateException msg)
			{
				MessageBox.Show(msg.ToString(), "Exception");
			} 
		}

		private void btnResume_Click(object sender, System.EventArgs e)
		{
			if(primeThread.ThreadState == ThreadState.Suspended ||
				primeThread.ThreadState == ThreadState.SuspendRequested)
			{
				try
				{
					// Resume the thread
					primeThread.Resume();
					// Disable the resume button
					btnResume.Enabled = false;
					// Enable the Pause button
					btnPause.Enabled = true;
					// Enable the Stop button
					btnStop.Enabled = true;
				}
				catch(ThreadStateException msg)
				{
					MessageBox.Show(msg.ToString(), "Exception");
				}
			}
		}

		public void Generate()
		{
			// true = prime number; false = not a prime number
			bool IsPrime = true; 
			// Holds the number of prime numbers generated
			int count = 2;
			// Since first prime is 2 it is added to the list
			lstPrime.Items.Add(2);

			// Iterate until 24 more prime numbers are generated.
			for(int i = 3; count < 50; i += 2)
			{
				IsPrime = true;
				// Iterate half the number of times of the number "i" 
				// to be checked
				for(int j = 2; j <= (i / 2); j++)
				{
					// Check if "i" is divisible 
					if(i % j == 0)
					{
						IsPrime = false;
						break;
					}
				}
				
				// Display the prime number 
				// and increment the counter
				if(IsPrime)
				{
					lstPrime.Items.Add(i);
					count++;
					// The thread is put to sleep for 100 milliseconds
					// to enable suspension and resumption of the						// thread
					Thread.Sleep(100);
				}
			}

			// Enable and disable buttons after the thread finishes				// execution
			btnStart.Enabled = true;
			btnPause.Enabled = false;
			btnStop.Enabled = false;
			btnResume.Enabled = false;
		}

		private void frmPrime_Load(object sender, System.EventArgs e)
		{
			btnPause.Enabled = false;
			btnResume.Enabled = false;
			btnStop.Enabled = false;
			Thread.CurrentThread.Priority = ThreadPriority.Highest;
		}

		private void frmPrime_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			try
			{
				primeThread.Abort(); 
			}
			catch(Exception Ee)
			{
				//Do nothing here
			}
		}
	}
}

⌨️ 快捷键说明

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