P177线程
1.通过进程去打开应用程序
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace _02线程和进程的复习
{
class Program
{
static void Main(string[] args)
{
//通过进程去打开应用程序
//Process.getprocesses
//Process.Start("notepad");
//Process.Start("iexplore", "http://www.baidu.com");
//通过进程去打开指定的文件
//ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\SpringRain\Desktop\1、播放音乐下一曲.wmv");
//Process p = new Process();
//p.StartInfo = psi;
//p.Start();
//Console.ReadKey();
//进程和线程的关系? 一个进程包含多个线程
//前台 后台
}
}
}
2.通过进程去打开指定的文件
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace _02线程和进程的复习
{
class Program
{
static void Main(string[] args)
{
//通过进程去打开应用程序
//Process.getprocesses
//Process.Start("notepad");
//Process.Start("iexplore", "http://www.baidu.com");
//通过进程去打开指定的文件
//ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\SpringRain\Desktop\1、播放音乐下一曲.wmv");
//Process p = new Process();
//p.StartInfo = psi;
//p.Start();
//Console.ReadKey();
//进程和线程的关系? 一个进程包含多个线程
//前台 后台
}
}
}
3.进程和线程的关系
一个进程包含多个线程
使用后天线程,解决假死问题
4.thread类
①start()启动线程
②Abort()终止线程,终止完后不能再Start
③Thread.Sleep(1)静态方法,可以使当前线程停止一段时间运行
5.防止界面假死问题
思路:创建一个线程,并设为后台线程,传参数
private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(Test); //创建一个线程
th.IsBackground = true; //设为后台线程
th.Start("123");//传参数
//Test();
}
private void Test(object s)//必须为object类型
{
string ss = (string)s; //object转为string
for (int i = 0; i < 10000; i++)
{
Console.WriteLine(i);
}
}
6.摇奖机程序
①click事件
bool b = false;
private void button1_Click(object sender, EventArgs e) { if (b == false) { b = true; button1.Text = "停止"; Thread th = new Thread(PlayGame); th.IsBackground = true; th.Name = "新线程"; // th. th.Start(); } else//b==true { b = false; button1.Text = "开始"; } //PlayGame(); }
②随机数生成
private void PlayGame() { Random r = new Random(); while (b) { label1.Text = r.Next(0, 10).ToString(); label2.Text = r.Next(0, 10).ToString(); label3.Text = r.Next(0, 10).ToString(); } }
③Tip:异常处理:线程间操作异常
private void Form1_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
}
④所有代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace _05_摇奖机应用程序 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } bool b = false; private void button1_Click(object sender, EventArgs e) { if (b == false) { b = true; button1.Text = "停止"; Thread th = new Thread(PlayGame); th.IsBackground = true; th.Name = "新线程"; // th. th.Start(); } else//b==true { b = false; button1.Text = "开始"; } //PlayGame(); } private void PlayGame() { Random r = new Random(); while (b) { label1.Text = r.Next(0, 10).ToString(); label2.Text = r.Next(0, 10).ToString(); label3.Text = r.Next(0, 10).ToString(); } } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; } } }
namespace _05_摇奖机应用程序 { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(111, 137); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 0; this.label1.Text = "label1"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(246, 137); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(41, 12); this.label2.TabIndex = 1; this.label2.Text = "label2"; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(396, 136); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(41, 12); this.label3.TabIndex = 2; this.label3.Text = "label3"; // // button1 // this.button1.Location = new System.Drawing.Point(398, 297); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 3; this.button1.Text = "开始"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(626, 456); this.Controls.Add(this.button1); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.Button button1; } }