一、directory類(對文件夾操作的類)
static void Main(string[] args)
{
//File操作文件 Path操作路徑 FileStream操作文件流 StreamReader和StreamWrite
//Directory 操作文件夾 目錄
////創建文件夾
//Directory.CreateDirectory(@"C:\a");
//Console.WriteLine("創建成功");
////刪除文件夾只能刪除空的,如果想刪除有東西的文件夾,在路徑後面加true
//Directory.Delete(@"C:\a",true);
//Console.WriteLine("刪除成功");
////剪切到其他位置
//Directory.Move(@"C:\a", @"C:\Users\Administrator\Desktop\new");
//Console.WriteLine("剪切成功");
////獲得指定文件夾下所有文件的全路徑
//string[] path = Directory.GetFiles(@"C:\Users\Administrator\Desktop\Poth", "*.jpg");
//for (int i = 0; i < path.Length; i++)
//{
// Console.WriteLine(path[i]);
//}
//Console.ReadKey();
// //獲得指定目錄下所有文件夾的全路徑
//string[] path = Directory.GetDirectories(@"C:\Users\Administrator\Desktop");
// for (int i = 0; i < path.Length; i++)
// {
// Console.WriteLine(path[i]);
// }
// Console.ReadKey();
////判斷指定的文件夾是否存在
//if (Directory.Exists(@"c:\a\b"))
//{
// for (int i = 0; i < 100; i++)
// {
// Directory.CreateDirectory(@"C:\a\b"+ i);
// }
//}
//Console.WriteLine("OK");
//Console.ReadKey();
}
二、瀏覽器控件(WebBrowser)
代碼定義
private void button1_Click(object sender, EventArgs e)
{
//1.接收用戶輸入到文本框
string text = textBox1.Text;
3.通過轉換,將用戶輸入的string 類型轉換爲uri類型
Uri uri = new Uri("http://"+text);
//2.將用戶輸入的內容放到Url屬性裏面
webBrowser1.Url = uri;
}
三、下拉框控件(comboBox):該類控件取名一般以:cbo+....
DropDownStyle:控制下拉框的外觀樣式
通過代碼添加和清空下拉框數據
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//通過代碼給下拉框添加數據
comboBox2.Items.Add("孫子");
comboBox2.Items.Add("嘲諷");
comboBox2.Items.Add("臉呢");
}
private void button2_Click(object sender, EventArgs e)
{
//情況下拉框數據
comboBox2.Items.Clear();
}
}
日期選擇器練習
1.拉三個comboBox控件
2.在form1的Load控件加載年份,使年份初始化就有
private void Form1_Load(object sender, EventArgs e)
{
//程序加載時,將年份加載到下拉框中
//首先需要獲得年份
int year = DateTime.Now.Year;
for (int i = year; i <= 1949; i--)
{
cboYear.Items.Add(i + "年");
}
}
3.在cboYear年份控件裏面使用SelectedIndexChanged事件,來加載第二個下拉框的月份
private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
{
//添加之前應該將之前的數據情況
cboMoon.Items.Clear();
for (int i = 1; i < 13; i++)
{
cboMoon.Items.Add(i + "月");
}
}
4.在第二個下拉框使用SelectedIndexChanged事件,來加載第三個下拉框的日期,由於日期不固定,且還有閏年,所以日期最複雜
/// <summary>
/// 當月份發生改變的時候 加載天
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cboMoon_SelectedIndexChanged(object sender, EventArgs e)
{
cboDay.Items.Clear();
int day = 0;//定義一個變量來存儲天數
//獲得月份
string strMonth = cboMoon.SelectedItem.ToString().Split(new char[] { '月' }, StringSplitOptions.RemoveEmptyEntries)[0];
//MessageBox.Show(cboMoon.SelectedItem.ToString());
//獲得年份
string strYear = cboYear.SelectedItem.ToString().Split(new char[] { '年' }, StringSplitOptions.RemoveEmptyEntries)[0];
//通過上面兩行代碼,我們就獲得了字符串類型的年份和月份,但是我們需要的是int類型的
int year = Convert.ToInt32(strYear);
int month = Convert.ToInt32(strMonth);
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:day = 31;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
day = 29;
}
else
{
day = 28;
}
break;
default:day = 30;
break;
}
for (int i = 1; i <= day; i++)
{
cboDay.Items.Add(i + "日");
}
}
全部代碼如下:
/// <summary>
/// 初始化年份
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
//程序加載時,將年份加載到下拉框中
//首先需要獲得年份
int year = DateTime.Now.Year;
for (int i = year; i >= 1949; i--)
{
cboYear.Items.Add(i + "年");
}
}
/// <summary>
/// 只有選擇年份後,纔會初始化月份,並且每選擇一次年份都會清空一次月份
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cboYear_SelectedIndexChanged(object sender, EventArgs e)
{
//添加之前應該將之前的數據情況
cboMoon.Items.Clear();
for (int i = 1; i < 13; i++)
{
cboMoon.Items.Add(i + "月");
}
}
/// <summary>
/// 當月份發生改變的時候 加載天
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cboMoon_SelectedIndexChanged(object sender, EventArgs e)
{
cboDay.Items.Clear();
int day = 0;//定義一個變量來存儲天數
//獲得月份
string strMonth = cboMoon.SelectedItem.ToString().Split(new char[] { '月' }, StringSplitOptions.RemoveEmptyEntries)[0];
//MessageBox.Show(cboMoon.SelectedItem.ToString());
//獲得年份
string strYear = cboYear.SelectedItem.ToString().Split(new char[] { '年' }, StringSplitOptions.RemoveEmptyEntries)[0];
//通過上面兩行代碼,我們就獲得了字符串類型的年份和月份,但是我們需要的是int類型的
int year = Convert.ToInt32(strYear);
int month = Convert.ToInt32(strMonth);
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:day = 31;
break;
case 2:
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
{
day = 29;
}
else
{
day = 28;
}
break;
default:day = 30;
break;
}
for (int i = 1; i <= day; i++)
{
cboDay.Items.Add(i + "日");
}
}
}
四、列表控件(ListBox)
DoubleClick事件:雙擊時發生
通過代碼添加數據
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add(1);
listBox1.Items.Add(2);
listBox1.Items.Add(3);
listBox1.Items.Add(4);
listBox1.Items.Add(5);
listBox1.Items.Add(6);
listBox1.Items.Add(7);
}
小練習:點擊更換圖片
1.在程序加載的時候,將制定圖片文件夾中所有的圖片文件名讀取到ListBox中
private void Form1_Load(object sender, EventArgs e)
{
string[] str = Directory.GetFiles(@"C:\Users\Administrator\Desktop\Poth","*.jpg");
for (int i = 0; i < str.Length; i++)
{
// listBox1.Items.Add(str[i]);
string file = Path.GetFileName(str[i]);
listBox1.Items.Add(file);
}
}
2。通過圖片路徑的索引拿到圖片,並創建雙擊點擊播放
string[] str = Directory.GetFiles(@"C:\Users\Administrator\Desktop\Poth", "*.jpg");
/// <summary>
/// 雙擊播放圖片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_DoubleClick(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile(str[listBox1.SelectedIndex]);
}
全部代碼如下
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//List<string> list = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < str.Length; i++)
{
// listBox1.Items.Add(str[i]);
string file = Path.GetFileName(str[i]);
listBox1.Items.Add(file);
//將圖片的全路徑添加到List泛型集合中
//list.Add(str[i]);
}
}
string[] str = Directory.GetFiles(@"C:\Users\Administrator\Desktop\Poth", "*.jpg");
/// <summary>
/// 雙擊播放圖片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_DoubleClick(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile(str[listBox1.SelectedIndex]);
}
}
還有一種寫法,通過List泛型集合來拿索引
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//通過List泛型集合來拿路徑索引
List<string> list = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
string[] str = Directory.GetFiles(@"C:\Users\Administrator\Desktop\Poth", "*.jpg");
for (int i = 0; i < str.Length; i++)
{
// listBox1.Items.Add(str[i]);
string file = Path.GetFileName(str[i]);
listBox1.Items.Add(file);
//將圖片的全路徑添加到List泛型集合中
list.Add(str[i]);
}
}
/// <summary>
/// 雙擊播放圖片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_DoubleClick(object sender, EventArgs e)
{
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox1.Image = Image.FromFile(list[listBox1.SelectedIndex]);
}
}
雙擊播放音樂小練習
第一步創建窗體,使用ListBox控件,並且把音樂文件夾路徑及文件名給ListBox控件
//存儲音樂文件的全路徑
List<string> listSongs = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
string[] path = Directory.GetFiles(@"C:\Users\Administrator\Desktop\music");
for (int i = 0; i < path.Length; i++)
{
string file = Path.GetFileName(path[i]);
listBox1.Items.Add(file);
//將音樂文件的全路徑存到泛型集合中
listSongs.Add(path[i]);
}
}
第二步:爲ListBox創建雙擊事件播放事件
private void listBox1_DoubleClick(object sender, EventArgs e)
{
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = listSongs[listBox1.SelectedIndex];
sp.Play();
}
石頭剪刀布(主要是結合winform及面向對象)
需求分析:首先分析出需要玩家、電腦、裁判三個類,其中各自包括自己的方法
玩法:
石頭用1表示 剪刀用2表示 布用3表示
玩家贏的情況 1-2=-1 2 -3=-1 3 -1 =2
平手 相減 = 0
另外一種情況:電腦贏了
1.創建玩家類
class Player
{
public int ShowFist(string fist)
{
//將玩家按的按鈕(出拳),轉換爲數字並返回
int num = 0;
switch (fist)
{
case "石頭": num = 1;
break;
case "剪刀":num = 2;
break;
case "布":num = 3;
break;
}
return num;
}
}
2.創建電腦類,電腦類是隨機產生的數字,這裏就需要把數字轉換爲出拳,並且存儲在自動屬性裏面待UI調用
class Computer
{
//用來存儲電腦出的拳
public string Fist
{
get;
set;
}
public int ShowFist()
{
Random r = new Random();
int rNumber = r.Next(1, 4);
//將電腦的隨機數(出拳)轉換成出的全,留待給lblComputer調用
switch (rNumber)
{
case 1:this.Fist = "石頭";
break;
case 2:this.Fist = "剪刀";
break;
case 3:this.Fist = "布";
break;
}
//返回產生的隨機數,提供給裁判去判斷輸贏
return rNumber;
}
}
3.裁判類,通過傳遞進來的玩家數字,電腦隨機數,判斷出結果
public enum Result
{
玩家贏,
電腦贏,
平手
}
class Judge
{
public static Result Judge1(int playerNumber, int computerNumber)
{
if (playerNumber - computerNumber == -1 || playerNumber - computerNumber == 2)
{
return Result.玩家贏;
}
else if (playerNumber - computerNumber == 0)
{
return Result.平手;
}
else
{
return Result.電腦贏;
}
}
}
4.創建玩遊戲的方法,前期創建在出的第一個拳的判斷內,然後單獨重構出一個方法
private void PlayerGame(string str)
{
//頭三行代碼:將玩家出拳賦值給lblPlayer以供顯示,
//並且把玩家的選擇傳遞給玩家類的ShowFist方法,以供將玩家出拳轉換爲數字。
lblPlayer.Text = str;
Player player = new Player();
int playerNumber = player.ShowFist(str);
//中間三行代碼:創建電腦對象,將電腦類中產生的隨機數賦值給賦值給int作爲參數
//將隨機數轉換成的出拳賦值給lblComputer,這裏調用的是存儲隨機數轉換成出拳的自動屬性
Computer cpu = new Computer();
int cpuNumber = cpu.ShowFist();
lblComputer.Text = cpu.Fist;
//通過傳遞給裁判類的玩家參數(玩家選擇出拳後轉換成的數字)
//傳遞給裁判類的電腦參數(電腦產生的隨機數)
//兩個參數按照裁判類中的方法對比,得出結果
Result res = Judge.Judge1(playerNumber, cpuNumber);
//將結果轉換成ToString,賦值給lblResult
lblResult.Text = res.ToString();
}
5.創建三個按鈕(石頭,剪刀,布)方法,調用玩法的方法進行遊戲
private void btnStone_Click(object sender, EventArgs e)
{
string str = "石頭";
PlayerGame(str);
}
private void btnCut_Click(object sender, EventArgs e)
{
string str = "剪刀";
PlayerGame(str);
}
private void btnCloth_Click(object sender, EventArgs e)
{
string str = "布";
PlayerGame(str);
}
整體代碼如下
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 重構的玩遊戲的方法
/// </summary>
/// <param name="str"></param>
private void PlayerGame(string str)
{
lblPlayer.Text = str;
Player player = new Player();
int playerNumber = player.ShowFist(str);
Computer cpu = new Computer();
int cpuNumber = cpu.ShowFist();
lblComputer.Text = cpu.Fist;
Result res = Judge.Judge1(playerNumber, cpuNumber);
lblResult.Text = res.ToString();
}
private void btnStone_Click(object sender, EventArgs e)
{
string str = "石頭";
PlayerGame(str);
}
private void btnCut_Click(object sender, EventArgs e)
{
string str = "剪刀";
PlayerGame(str);
}
private void btnCloth_Click(object sender, EventArgs e)
{
string str = "布";
PlayerGame(str);
}
}
此題難點在於分析需求及玩法
五、對話框(打開文件)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//創建點擊彈出對話框對象
OpenFileDialog ofd = new OpenFileDialog();
//設置對話框的標題
ofd.Title = "請選擇要打開的文本文件喲!!";
//設置對話框可以多選
ofd.Multiselect = true;
//設置對話框的初始目錄
ofd.InitialDirectory = @"C:\Users\Administrator\Desktop";
//設置對話框的文件類型
ofd.Filter = "文本文件|*.txt|媒體文件|*.wmv|圖片文件|*.jpg|所有文件|*.*";
//展示對話框
ofd.ShowDialog();
//獲得在打開對話框中選中文件的路徑
string path = ofd.FileName;
//判斷用戶有沒有選擇文件
if (path == "")
{
return;
}
using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 5];
//實際讀取到的字節數
int r = fsRead.Read(buffer, 0, buffer.Length);
//返回給需要顯示的控件
textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
}
}
}
對話框(保存文件)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//創建保存對話框的對象
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "請選擇要保存的路徑";
sfd.InitialDirectory = @"C:\Users\Administrator\Desktop";
sfd.Filter = "文本文件|*.txt|所有文件|*.*";
//保存文件不能設置多個路徑,只能在一個路徑下
sfd.ShowDialog();
//拿到保存文件的路徑
string path = sfd.FileName;
if (path == "")
{
return;
}
using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
fsWrite.Write(buffer, 0, buffer.Length);
}
MessageBox.Show("寫入成功");
}
}
對話框(字體和顏色)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 字體對話框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.ShowDialog();
textBox1.Font = fd.Font;
}
/// <summary>
/// 顏色對話框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.ShowDialog();
textBox1.ForeColor = cd.Color;
}
}
六、容器控件(Panel)
記事本項目練習,相當於十四十五天的集合
代碼如下
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = true;
}
private void Form1_Load(object sender, EventArgs e)
{
//加載程序的時候 隱藏panel
panel1.Visible = false;
//取消文本框的自動換行
textBox1.WordWrap = false;
}
//使用泛型集合來拿到打開文件的全路徑
List<string> list = new List<string>();
/// <summary>
/// 點擊按鈕的時候 隱藏panel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click_1(object sender, EventArgs e)
{
panel1.Visible = false;
}
private void 顯示ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible = true;
}
private void 隱藏ToolStripMenuItem_Click(object sender, EventArgs e)
{
panel1.Visible = false;
}
/// <summary>
/// 打開對話框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 打開ToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "請選擇要打開的文本文件";
ofd.InitialDirectory = @"C:\Users\Administrator\Desktop";
ofd.Multiselect = true;
ofd.Filter = "文本文件|*.txt|所有文件|*.*";
ofd.ShowDialog();
//獲得用戶選中的文件的路徑
string path = ofd.FileName;
//將文件的全路徑存儲到泛型集合中
list.Add(path);
//獲得用戶打開文件的文件名
string fileName = Path.GetFileName(path);
//將文件名放到ListBox中
listBox1.Items.Add(fileName);
if (path == "")
{
return;
}
using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 5];
int r = fsRead.Read(buffer, 0, buffer.Length);
textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
}
}
/// <summary>
/// 保存對話框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.InitialDirectory = @"C:\Users\Administrator\Desktop";
sfd.Filter = "文本文件|*.txt|所有文件|*.*";
sfd.ShowDialog();
string path = sfd.FileName;
if (path == "")
{
return;
}
using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = Encoding.Default.GetBytes(textBox1.Text);
fsWrite.Write(buffer, 0, buffer.Length);
}
MessageBox.Show("保存成功");
}
/// <summary>
/// 自動換行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 自動換行ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (自動換行ToolStripMenuItem.Text == "☆自動換行")
{
textBox1.WordWrap = true;
自動換行ToolStripMenuItem.Text = "☆取消自動換行";
}
else
{
textBox1.WordWrap = false;
自動換行ToolStripMenuItem.Text = "☆自動換行";
}
}
/// <summary>
/// 字體
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 字體ToolStripMenuItem_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
fd.ShowDialog();
textBox1.Font = fd.Font;
}
/// <summary>
/// 顏色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void 顏色ToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
cd.ShowDialog();
textBox1.ForeColor = cd.Color;
}
/// <summary>
/// 雙擊打開對應的文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_DoubleClick(object sender, EventArgs e)
{
//要獲得雙擊的文件所對應的全路徑
string path = list[listBox1.SelectedIndex];
using (FileStream fsRead = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 5];
int r = fsRead.Read(buffer, 0, buffer.Length);
textBox1.Text = Encoding.Default.GetString(buffer, 0, r);
}
}
}
七、進程類,關鍵字(Process)
計算機中每一個運行的應用程序都當做是一個進程。
而一個進程又是由多個線程組成的。
常用方法
static void Main(string[] args)
{
////獲得當前程序中所有正在運行的進程
//Process[] pros = Process.GetProcesses();
//foreach (var item in pros)
//{
// //殺死進程
// //item.Kill()
// Console.WriteLine(item);
//}
////通過進程打開一些應用程序
//Process.Start("calc");
//Process.Start("iexplore","http://www.baidu.com");
//通過進程打開指定的文件
//第二:找到StarInfo
ProcessStartInfo psi = new ProcessStartInfo(@"C:\Users\Administrator\Desktop\違規詞.txt");
// 第一:創建進程對象
Process p = new Process();
p.StartInfo = psi;
p.Start();
Console.ReadKey();
}
八、多線程 關鍵字(Thread)
單個線程給我們帶來的問題:只能同時執行一個操作,必須等這個操作執行完,才能執行下一個操作,造成窗體假死。
前臺線程:只有所有的前臺線程都關閉才能完成程序關閉。
後臺線程:只要所有的前臺線程結束,後臺線程自動結束。
.net下是不允許跨線程訪問的
當線程被關閉:Abort()就不可以在被打開:Start()
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread th;
private void button1_Click(object sender, EventArgs e)
{
//創建一個線程去執行這個方法
th = new Thread(Test);
//標記這個線程準備就緒,可以隨時被執行,具體什麼時候執行,由CPU決定
//將線程設置爲後臺線程
th.IsBackground = true;
th.Start();
}
private void Test()
{
for (int i = 0; i < 10000; i++)
{
//Console.WriteLine(i);
textBox1.Text = i.ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
//取消跨線程訪問
Control.CheckForIllegalCrossThreadCalls = false;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//當你點擊關閉窗體的時候,判斷新線程是否 =null
if (th != null)
{
//結束這個線程
th.Abort();
}
}
}
暫停線程
static void Main(string[] args)
{
//線程暫停,()內爲暫停時間,以毫秒爲單位
Thread.Sleep(3000);
Console.WriteLine("hello word");
Console.ReadKey();
}