日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

运行管理员线程和用户线程小练习

發布時間:2025/5/22 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 运行管理员线程和用户线程小练习 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

今天學習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

總結

以上是生活随笔為你收集整理的运行管理员线程和用户线程小练习的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。