C#基础【十四】

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Base14
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 给线程取名字
            //Thread.CurrentThread.Name = "当前运行的线程";//Thread.CurrentThread.Name 
            //Console.WriteLine(Thread.CurrentThread.Name);
            #endregion

            #region IsBackground 判断是否是后台类型
            //Console.WriteLine(Thread.CurrentThread.IsBackground);
            //Console.ReadKey();
            #endregion

            #region 获取当前正在被执行的进程
            //Console.WriteLine(Process.GetCurrentProcess().ProcessName);
            //Console.ReadKey();
            #endregion

            #region 获取到输出当前环境下所有的进程
            //foreach (Process item in Process.GetProcesses())
            //{
            //    Console.WriteLine(item.ProcessName);
            //}
            //Console.ReadKey();
            #endregion

            #region 通过命令打开进程 ie
            //foreach (Process item in Process.GetProcesses())
            //{
            //    if (item.ProcessName.Contains("ie"))
            //    {
            //        Console.WriteLine(item.ProcessName);
            //    }
            //}
            //Console.ReadKey();
            //Process.Start("iexplore","http://www.baidu.com");
            #endregion

            #region 挂起、唤醒 
            //ThreadStart ts = new ThreadStart(Show);//ThreadStart 委托
            //Thread myThread = new Thread(ts);
            //myThread.Name = "newThread";
            //myThread.Start();//执行本线程
            //myThread.Abort();//终止本线程
            挂起 先不执行
            //myThread.Suspend();
            //Console.WriteLine("哈哈哈");
            唤醒 执行线程
            //myThread.Resume();
            //Console.ReadKey();
            #endregion

            #region 销毁线程
            //Thread mythread = new Thread(ThreadMethod);
            //mythread.Start();
            //Thread.Sleep(2000);
            //mythread.Abort();
            //Console.ReadKey();
            #endregion

            #region 创建线程
            //Thread mt = new Thread(ThreadMethod);
            //mt.Start("我是一个参数");
            //Console.ReadKey();
            #endregion

            #region 域
            //Console.WriteLine(Thread.GetDomain().FriendlyName);
            //Console.WriteLine(Thread.GetDomainID());
            //Console.ReadKey();
            #endregion

            #region 中断
            //Thread mt = new Thread(ThreadM);
            //mt.Start();
            //Thread.Sleep(200);
            //mt.Interrupt();
            //Thread.Sleep(200);
            //mt.Join();
            //Console.ReadKey();
            #endregion

            #region 线程同步
            //Book book = new Book();
            //Thread t1 = new Thread(book.Sale);
            //t1.Name = "第一个线程";
            //Thread t2 = new Thread(book.Sale);
            //t2.Name = "第二个线程";
            //t1.Start();
            //t2.Start();
            //Console.ReadKey();
            #endregion

            #region 确保生成无重复的数字
            //for (int i = 0; i < 5; i++)
            //{
            //    Thread thread = new Thread(MyRandom);
            //    thread.Start();
            //}
            //Console.ReadKey();
            #endregion

            #region 确保生成无重复的数字 长
            //for (int i = 0; i < 5; i++)
            //{
            //    Thread thread = new Thread(MyRandom1);
            //    thread.Start();
            //}
            //Console.ReadKey();
            #endregion

        }
        #region Show()
        //public static void Show()
        //{
        //    Console.WriteLine("这是一个单独的线程");
        //}
        #endregion

        #region Show1()
        //public static void Show1()
        //{
        //    Console.WriteLine("独立休眠5秒");
        //    Thread.Sleep(5000);
        //}
        #endregion

        #region ThreadMethod()
        //public static void ThreadMethod()
        //{
        //    try
        //    {
        //        for (int i = 0; i < 10; i++)
        //        {
        //            Thread.Sleep(500);
        //            Console.WriteLine(i);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine("执行的线程被强制终止");
        //    }           
        //}
        #endregion

        #region ThreadMethod(object objects)
        //public static void ThreadMethod(object objects)
        //{
        //    try
        //    {
        //        for (int i = 0; i < 10; i++)
        //        {
        //            Console.WriteLine(i);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine("执行的线程被强制终止");
        //    }
        //}
        #endregion

        #region ThreadM()
        //public static void 
        //{
        //    try
        //    {
        //        for (int i = 0; i < 10; i++)
        //        {
        //            Thread.Sleep(100);
        //            Console.WriteLine(i);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Console.WriteLine("执行的线程被强制中断");
        //    }
        //}
        #endregion

        #region MyRandom()
        //public static void MyRandom()
        //{
        //    Random r = new Random();
        //    int index = r.Next(0, 99);
        //    Console.WriteLine(index);
        //}
        #endregion

        #region MyRandom1()
        //public static void MyRandom1()
        //{
        //    byte[] bytes = Guid.NewGuid().ToByteArray();
        //    int index = BitConverter.ToInt32(bytes, 0);
        //    if (index < 0)
        //    {
        //        //递归 自己调用自己
        //        MyRandom1();
        //    }
        //    else
        //    {
        //        Random r = new Random();
        //        Console.WriteLine(r.Next(index)/10000);
        //    }          
        //}
        #endregion

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值