多线程
作者:互联网
1.进程
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace _15进程 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 //获得当前程序中所有正在运行的进程 15 //Process[] pros = Process.GetProcesses(); 16 //foreach (var item in pros) 17 //{ 18 // //不试的不是爷们 19 // //item.Kill(); 20 // Console.WriteLine(item); 21 //} 22 23 //通过进程打开一些应用程序 24 //Process.Start("calc"); 25 //Process.Start("mspaint"); 26 //Process.Start("notepad"); 27 //Process.Start("iexplore", "http://www.baidu.com"); 28 29 //通过一个进程打开指定的文件 30 31 ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\SpringRain\Desktop\1.exe"); 32 33 //第一:创建进程对象 34 Process p = new Process(); 35 p.StartInfo = psi; 36 p.Start(); 37 // p.star 38 39 40 Console.ReadKey(); 41 } 42 } 43 }
2.线程
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 using System.Threading.Tasks; 7 8 namespace _16_线程 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 Thread.Sleep(3000); 15 Console.WriteLine("Hello World"); 16 Console.ReadKey(); 17 } 18 } 19 }
3.线程
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace _17_线程 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 Thread th; 21 private void button1_Click(object sender, EventArgs e) 22 { 23 //创建一个线程去执行这个方法 24 th = new Thread(Test); 25 //标记这个线程准备就绪了,可以随时被执行。具体什么时候执行这个线程, 26 //由cpu决定 27 //将线程设置为后台线程 28 th.IsBackground = true; 29 th.Start(); 30 th.Abort(); 31 th.Start(); 32 33 } 34 35 private void Test() 36 { 37 for (int i = 0; i < 10000; i++) 38 { 39 //Console.WriteLine(i); 40 textBox1.Text = i.ToString(); 41 } 42 } 43 44 private void Form1_Load(object sender, EventArgs e) 45 { 46 //取消跨线程的访问 47 Control.CheckForIllegalCrossThreadCalls = false; 48 } 49 50 private void Form1_FormClosing(object sender, FormClosingEventArgs e) 51 { 52 //当你点击关闭窗体的时候,判断新线程是否为null 53 if (th != null) 54 { 55 //结束这个线程 56 th.Abort(); 57 } 58 } 59 60 } 61 }
1 namespace _17_线程 2 { 3 partial class Form1 4 { 5 /// <summary> 6 /// 必需的设计器变量。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 清理所有正在使用的资源。 12 /// </summary> 13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows 窗体设计器生成的代码 24 25 /// <summary> 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.button1 = new System.Windows.Forms.Button(); 32 this.textBox1 = new System.Windows.Forms.TextBox(); 33 this.SuspendLayout(); 34 // 35 // button1 36 // 37 this.button1.Location = new System.Drawing.Point(168, 70); 38 this.button1.Name = "button1"; 39 this.button1.Size = new System.Drawing.Size(75, 23); 40 this.button1.TabIndex = 0; 41 this.button1.Text = "button1"; 42 this.button1.UseVisualStyleBackColor = true; 43 this.button1.Click += new System.EventHandler(this.button1_Click); 44 // 45 // textBox1 46 // 47 this.textBox1.Location = new System.Drawing.Point(168, 137); 48 this.textBox1.Name = "textBox1"; 49 this.textBox1.Size = new System.Drawing.Size(311, 21); 50 this.textBox1.TabIndex = 1; 51 // 52 // Form1 53 // 54 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 55 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 56 this.ClientSize = new System.Drawing.Size(638, 390); 57 this.Controls.Add(this.textBox1); 58 this.Controls.Add(this.button1); 59 this.Name = "Form1"; 60 this.Text = "Form1"; 61 this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); 62 this.Load += new System.EventHandler(this.Form1_Load); 63 this.ResumeLayout(false); 64 this.PerformLayout(); 65 66 } 67 68 #endregion 69 70 private System.Windows.Forms.Button button1; 71 private System.Windows.Forms.TextBox textBox1; 72 } 73 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace _17_线程 8 { 9 static class Program 10 { 11 /// <summary> 12 /// 应用程序的主入口点。 13 /// </summary> 14 [STAThread] 15 static void Main() 16 { 17 Application.EnableVisualStyles(); 18 Application.SetCompatibleTextRenderingDefault(false); 19 Application.Run(new Form1()); 20 } 21 } 22 }
4.线程执行带参数的方法
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace _04_线程执行带参数的方法 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 21 private void button1_Click(object sender, EventArgs e) 22 { 23 Thread th = new Thread(Test); 24 th.IsBackground = true; 25 th.Start("123"); 26 //Test(); 27 } 28 29 30 private void Test(object s) 31 { 32 string ss = (string)s; 33 for (int i = 0; i < 10000; i++) 34 { 35 Console.WriteLine(i); 36 } 37 } 38 } 39 }
1 namespace _04_线程执行带参数的方法 2 { 3 partial class Form1 4 { 5 /// <summary> 6 /// 必需的设计器变量。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 清理所有正在使用的资源。 12 /// </summary> 13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows 窗体设计器生成的代码 24 25 /// <summary> 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.button1 = new System.Windows.Forms.Button(); 32 this.SuspendLayout(); 33 // 34 // button1 35 // 36 this.button1.Location = new System.Drawing.Point(226, 28); 37 this.button1.Name = "button1"; 38 this.button1.Size = new System.Drawing.Size(75, 23); 39 this.button1.TabIndex = 0; 40 this.button1.Text = "button1"; 41 this.button1.UseVisualStyleBackColor = true; 42 this.button1.Click += new System.EventHandler(this.button1_Click); 43 // 44 // Form1 45 // 46 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 47 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 48 this.ClientSize = new System.Drawing.Size(599, 417); 49 this.Controls.Add(this.button1); 50 this.Name = "Form1"; 51 this.Text = "Form1"; 52 this.ResumeLayout(false); 53 54 } 55 56 #endregion 57 58 private System.Windows.Forms.Button button1; 59 } 60 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace _04_线程执行带参数的方法 8 { 9 static class Program 10 { 11 /// <summary> 12 /// 应用程序的主入口点。 13 /// </summary> 14 [STAThread] 15 static void Main() 16 { 17 Application.EnableVisualStyles(); 18 Application.SetCompatibleTextRenderingDefault(false); 19 Application.Run(new Form1()); 20 } 21 } 22 }
5.摇奖机程序
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace _05_摇奖机应用程序 13 { 14 public partial class Form1 : Form 15 { 16 public Form1() 17 { 18 InitializeComponent(); 19 } 20 bool b = false; 21 private void button1_Click(object sender, EventArgs e) 22 { 23 if (b == false) 24 { 25 b = true; 26 button1.Text = "停止"; 27 Thread th = new Thread(PlayGame); 28 th.IsBackground = true; 29 th.Name = "新线程"; 30 // th. 31 th.Start(); 32 } 33 else//b==true 34 { 35 b = false; 36 button1.Text = "开始"; 37 } 38 //PlayGame(); 39 } 40 private void PlayGame() 41 { 42 Random r = new Random(); 43 while (b) 44 { 45 label1.Text = r.Next(0, 10).ToString(); 46 label2.Text = r.Next(0, 10).ToString(); 47 label3.Text = r.Next(0, 10).ToString(); 48 } 49 } 50 51 private void Form1_Load(object sender, EventArgs e) 52 { 53 Control.CheckForIllegalCrossThreadCalls = false; 54 } 55 } 56 }
1 namespace _05_摇奖机应用程序 2 { 3 partial class Form1 4 { 5 /// <summary> 6 /// 必需的设计器变量。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 清理所有正在使用的资源。 12 /// </summary> 13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows 窗体设计器生成的代码 24 25 /// <summary> 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.label1 = new System.Windows.Forms.Label(); 32 this.label2 = new System.Windows.Forms.Label(); 33 this.label3 = new System.Windows.Forms.Label(); 34 this.button1 = new System.Windows.Forms.Button(); 35 this.SuspendLayout(); 36 // 37 // label1 38 // 39 this.label1.AutoSize = true; 40 this.label1.Location = new System.Drawing.Point(111, 137); 41 this.label1.Name = "label1"; 42 this.label1.Size = new System.Drawing.Size(41, 12); 43 this.label1.TabIndex = 0; 44 this.label1.Text = "label1"; 45 // 46 // label2 47 // 48 this.label2.AutoSize = true; 49 this.label2.Location = new System.Drawing.Point(246, 137); 50 this.label2.Name = "label2"; 51 this.label2.Size = new System.Drawing.Size(41, 12); 52 this.label2.TabIndex = 1; 53 this.label2.Text = "label2"; 54 // 55 // label3 56 // 57 this.label3.AutoSize = true; 58 this.label3.Location = new System.Drawing.Point(396, 136); 59 this.label3.Name = "label3"; 60 this.label3.Size = new System.Drawing.Size(41, 12); 61 this.label3.TabIndex = 2; 62 this.label3.Text = "label3"; 63 // 64 // button1 65 // 66 this.button1.Location = new System.Drawing.Point(398, 297); 67 this.button1.Name = "button1"; 68 this.button1.Size = new System.Drawing.Size(75, 23); 69 this.button1.TabIndex = 3; 70 this.button1.Text = "开始"; 71 this.button1.UseVisualStyleBackColor = true; 72 this.button1.Click += new System.EventHandler(this.button1_Click); 73 // 74 // Form1 75 // 76 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 77 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 78 this.ClientSize = new System.Drawing.Size(626, 456); 79 this.Controls.Add(this.button1); 80 this.Controls.Add(this.label3); 81 this.Controls.Add(this.label2); 82 this.Controls.Add(this.label1); 83 this.Name = "Form1"; 84 this.Text = "Form1"; 85 this.Load += new System.EventHandler(this.Form1_Load); 86 this.ResumeLayout(false); 87 this.PerformLayout(); 88 89 } 90 91 #endregion 92 93 private System.Windows.Forms.Label label1; 94 private System.Windows.Forms.Label label2; 95 private System.Windows.Forms.Label label3; 96 private System.Windows.Forms.Button button1; 97 } 98 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace _05_摇奖机应用程序 8 { 9 static class Program 10 { 11 /// <summary> 12 /// 应用程序的主入口点。 13 /// </summary> 14 [STAThread] 15 static void Main() 16 { 17 Application.EnableVisualStyles(); 18 Application.SetCompatibleTextRenderingDefault(false); 19 Application.Run(new Form1()); 20 } 21 } 22 }
标签:Windows,Form1,System,button1,new,using,多线程 来源: https://www.cnblogs.com/xhj8/p/15818456.html