运行管理员线程和用户线程小练习
?
今天學習C#中線程的知識點,于是動手編些有關線程的小例子,用來理解線程的運行原理,希望能夠幫助大家盡快入手
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConTest
{
??? class Program
{
??????? static void Main(string[] args)
??????? {
??????????? while (true)
??????????? {
??????????????? Display();
??????????????? RunThread();
??????????????? Console.Clear();
??????????? }????
???????????
?????????
?????
??????????? Console.Read();
??????? }
??????? private static void RunThread()
??????? {
??????????? int input = Convert.ToInt32(Console.ReadLine());
??????????? ThreadSample ts = new ThreadSample();
??????????? switch (input)
??????????? {
??????????????? case 1:
??????????????????? ts.ThreadStartToWork(ThreadSample.ThreadCatagory.AdminThread);
??????????????????? break;
??????????????? case 2:
??????????????????? ts.ThreadStartToWork(ThreadSample.ThreadCatagory.UserThread);
??????????????????? break;
??????????????? default:
??????????????????? //Thread.CurrentThread.Abort();??????????????????
??????????????????? System.Diagnostics.Process.GetCurrentProcess().Kill();
??????????????????? break;
??????????? }
??????? }
??????? private static void Display()
??????? {
??????????? Console.WriteLine("*******************************************");
??????????? for (int i = 0; i < 7; i++)
??????????? {
??????????????? if (i == 3)
??????????????? {
??????????????????? Console.WriteLine("*???????? 1、管理員線程工作?????????????? *");
??????????????????? Console.WriteLine("*???????????????????????????????????????? *");
??????????????? }
??????????????? else if (i == 4)
??????????????? {
??????????????????? Console.WriteLine("*???????? 2、普通用戶線程工作???????????? *");
??????????????????? Console.WriteLine("*???????????????????????????????????????? *");
??????????????????? Console.WriteLine("*???????? 3、輸入0退出程序??????????????? *");
??????????????????? Console.WriteLine("*???????????????????????????????????????? *");
??????????????? }
??????????????? else
??????????????? {
??????????????????? Console.WriteLine("*???????????????????????????????????????? *");
??????????????? }
??????????? }
??????????? Console.WriteLine("*******************************************");
??????????? Console.WriteLine("請選擇你需要的工作線程:");
??????? }
??????? static void ShowMsg()
??????? {
??????? }
??????????
???????
??? }
??? class ThreadSample
??? {
??????? public enum ThreadCatagory
??????? {
??????????? AdminThread,
??????????? UserThread
??????? }
??????? ThreadStart s;
??????? Thread t;
???????
??????? public void ThreadStartToWork(ThreadCatagory start)
??????? {
??????????? switch (start)
??????????? {
???????????????????
??????????????? case ThreadCatagory.AdminThread:
??????????????????? s = new ThreadStart(AdminThread);
??????????????????? break;
??????????????? case ThreadCatagory.UserThread:
??????????????????? s = new ThreadStart(UserThread);
??????????????????? break;
??????????????? default:
??????????????????? break;
??????????? }
?????????? t = new Thread(s);
?????????
?????????? t.Start();
??????????? t.Join();//阻塞調用線程,直到當前線程執行完畢,再執行調用線程(這是關鍵點否則出現不合邏輯的結果)
??????? }
??????? public void AdminThread()
??????? {
??????????? Console.WriteLine("管理員線程正在啟動......");
??????????? Thread.Sleep(5000);
??????????? Console.WriteLine("管理員線程正在運行中,請顧客耐心等等!!!!");
??????????? Thread.Sleep(5000);
??????????? Console.WriteLine("管理員線程正在結束,倒計時開始...");
??????????? for (int i = 5; i >=1; i--)
???{
???
??????????????? Thread.Sleep(1000);
??????????????? Console.WriteLine(i);
???}
??????????? Console.WriteLine("管理員線程已經結束了");????????
??????????? Thread.CurrentThread.Abort();
??????? }
??????? public void UserThread()
??????? {
??????????? Console.WriteLine("用戶線程正在啟動......");
??????????? Thread.Sleep(5000);
??????????? Console.WriteLine("用戶線程正在運行中,請顧客耐心等等!!!!");
??????????? Thread.Sleep(5000);
??????????? Console.WriteLine("用戶線程正在結束,倒計時開始...");
??????????? for (int i = 5; i >= 1; i--)
??????????? {
??????????????? Thread.Sleep(1000);
??????????????? Console.WriteLine(i);
??????????? }
??????????? Console.WriteLine("用戶線程已經結束了");????????
??????????? Thread.CurrentThread.Abort();
??????? }
??? }
}
轉載于:https://www.cnblogs.com/ganquanfu2008/archive/2013/03/22/2975759.html
總結
以上是生活随笔為你收集整理的运行管理员线程和用户线程小练习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 使用 UTF-8 编码(转
- 下一篇: JAVA基础加强(张孝祥)_类加载器、分