zoukankan      html  css  js  c++  java
  • C# 线程学习记录

    1. 线程的创建,启动,暂停,恢复和中止;

    Thread th = new Thread(ThreadMethod);  // 创建线程
            static void ThreadMethod()
            {
                while(true)
                {
                    Console.WriteLine("Thread executing......
    ");
                    Thread.Sleep(1000);
                }
            }
            static void Main(string[] args)
            {
                Console.WriteLine("Main start...
    ");
                Thread th = new Thread(ThreadMethod);  // 创建线程
                th.Start();    // 启动线程
                for (int i = 0; i < 10; i++)
                {
                    if (i == 3)
                        th.Suspend();   // 挂起线程
                    if (i == 5)
                        th.Resume();   // 继续线程
                    if (i == 7)
                        th.Abort();    // 中止线程
                    Console.WriteLine(i + "
    ");
                    Thread.Sleep(1000);
                }
                Console.WriteLine("Main end 
    ");
                Console.ReadLine();
            }

    Suspend 和 Resume 提示如下信息:

    修复该提示,新增一个 AutoResetEvent,修改代码如下:

    class Program
        {
            private static bool State = true;
            static AutoResetEvent ResetThr = new AutoResetEvent(false);
    
            public static void Stop()
            {
                State = false;
            }
            public static void Continue()
            {
                State = true;
                ResetThr.Set();
            }
            public static void Add()
            {
                while (true)
                {
                    for (int i = 0; i < 1000; i++)
                    {
                        if (!State)
                        {
                            ResetThr.WaitOne();
                            //// to suspend thread.
                            //ResetThr.Reset();
                        }
                        if(i==20)
                        {
                            Stop();
                            Console.WriteLine("Stop thread");
                        }
                        Console.WriteLine(i);
                        Thread.Sleep(500);
                        i++;
                    }
                }
            }
            static void ThreadMethod()
            {
                while(true)
                {
                    if (!State)
                    {
                        Thread.Sleep(3000);
                        Continue();
                        Console.WriteLine("Resume thread");
                    }
                    Console.WriteLine("Thread executing......");
                    Thread.Sleep(1000);
                }
            }
    
            static void Main(string[] args)
            {
                Console.WriteLine("Main start...");
                Thread th = new Thread(ThreadMethod);  // 创建线程
                Thread th2 = new Thread(Add);
                th2.Start();
                th.Start();    // 启动线程
                Console.WriteLine("Main end");
                Console.ReadLine();
            }
        }
  • 相关阅读:
    Virtual Judge —— Nim TopCoder
    Partial Sums ZOJ
    Partial Sums ZOJ
    Areas on the Cross-Section Diagram Aizu
    Areas on the Cross-Section Diagram Aizu
    Doubly Linked List Aizu
    Doubly Linked List Aizu
    1134:合法C标识符查
    TCP阻塞模式开发
    TCP阻塞模式开发
  • 原文地址:https://www.cnblogs.com/runningRain/p/13714925.html
Copyright © 2011-2022 走看看